"""Monitoring schemas for Mission Control API."""

from __future__ import annotations

from datetime import datetime
from uuid import UUID

from sqlmodel import SQLModel

from app.schemas.pagination import DefaultLimitOffsetPage


class CostSnapshotRead(SQLModel):
    """Cost snapshot read payload."""

    id: UUID
    organization_id: UUID
    gateway_id: UUID
    period_start: datetime
    period_end: datetime
    total_cost: float
    model_costs: dict | None = None
    provider_costs: dict | None = None
    token_counts: dict | None = None
    collected_at: datetime
    created_at: datetime
    updated_at: datetime


class CronJobStatusRead(SQLModel):
    """Cron job status read payload."""

    id: UUID
    organization_id: UUID
    gateway_id: UUID
    job_name: str
    schedule: str
    enabled: bool
    last_run_at: datetime | None = None
    next_run_at: datetime | None = None
    status: str
    failure_count: int
    last_error: str | None = None
    metadata_: dict | None = None
    created_at: datetime
    updated_at: datetime


class SessionEventRead(SQLModel):
    """Session event read payload."""

    id: UUID
    organization_id: UUID
    gateway_id: UUID
    session_key: str
    event_type: str
    model: str | None = None
    agent_id: str | None = None
    channel: str | None = None
    context_percent: float | None = None
    token_counts: dict | None = None
    cost: float | None = None
    metadata_: dict | None = None
    created_at: datetime
    updated_at: datetime


class SubAgentRunRead(SQLModel):
    """Sub-agent run read payload."""

    id: UUID
    organization_id: UUID
    gateway_id: UUID
    parent_session_key: str
    session_event_id: UUID | None = None
    agent: str | None = None
    model: str | None = None
    status: str
    duration_ms: int | None = None
    cost: float | None = None
    token_counts: dict | None = None
    metadata_: dict | None = None
    created_at: datetime
    updated_at: datetime


class SystemHealthMetricRead(SQLModel):
    """System health metric read payload."""

    id: UUID
    organization_id: UUID
    gateway_id: UUID
    cpu_percent: float | None = None
    cpu_cores: int | None = None
    ram_used_bytes: int | None = None
    ram_total_bytes: int | None = None
    ram_percent: float | None = None
    swap_used_bytes: int | None = None
    swap_total_bytes: int | None = None
    swap_percent: float | None = None
    disk_path: str
    disk_used_bytes: int | None = None
    disk_total_bytes: int | None = None
    disk_percent: float | None = None
    gateway_live: bool | None = None
    gateway_ready: bool | None = None
    gateway_uptime_ms: int | None = None
    gateway_pid: int | None = None
    gateway_version: str | None = None
    metadata_: dict | None = None
    collected_at: datetime
    created_at: datetime
    updated_at: datetime


class CostSummaryRead(SQLModel):
    """Cost summary payload - consolidated cost overview per gateway."""

    gateway_id: UUID
    gateway_name: str
    period_start: datetime
    period_end: datetime
    total_cost: float
    cost_by_model: dict[str, float]
    token_counts: dict[str, int] | None = None
    last_collected_at: datetime


class CostBreakdownRead(SQLModel):
    """Cost breakdown payload - models ranked by cost."""

    gateway_id: UUID
    gateway_name: str
    period_start: datetime
    period_end: datetime
    total_cost: float
    breakdown: list[dict[str, float]]  # [{"model": "claude-opus", "cost": 1.23, "percent": 45.6}]
    last_collected_at: datetime
