"""Response schemas for gateway operational visibility endpoints.

Covers: cron job status, system health snapshots, and health history.
All fields are optional — partial data is expected when the gateway
returns unexpected formats or a field is simply absent.
"""

from __future__ import annotations

from datetime import datetime
from uuid import UUID

from sqlmodel import SQLModel

RUNTIME_ANNOTATION_TYPES = (datetime, UUID)


# ---------------------------------------------------------------------------
# Cron
# ---------------------------------------------------------------------------

class CronJobRead(SQLModel):
    """One cron job entry returned by GET /gateways/{id}/cron."""

    name: str
    schedule: str = ""
    enabled: bool = True
    status: str                      # "ok" | "error" | "running" | "pending" | "disabled" | "unknown"
    last_run: str | None = None
    next_run: str | None = None
    last_duration_ms: int | None = None
    last_error: str | None = None


class CronStatusResponse(SQLModel):
    """Response for GET /gateways/{id}/cron."""

    gateway_id: UUID
    generated_at: datetime
    jobs: list[CronJobRead]


# ---------------------------------------------------------------------------
# Health
# ---------------------------------------------------------------------------

class HealthSnapshotRead(SQLModel):
    """One system health reading."""

    recorded_at: datetime
    cpu_pct: float | None = None
    memory_pct: float | None = None
    memory_used_bytes: int | None = None
    memory_total_bytes: int | None = None
    disk_pct: float | None = None
    disk_used_bytes: int | None = None
    disk_total_bytes: int | None = None
    uptime_seconds: int | None = None
    load_avg_1m: float | None = None
    load_avg_5m: float | None = None
    load_avg_15m: float | None = None
    hostname: str | None = None
    platform: str | None = None


class SystemHealthResponse(SQLModel):
    """Response for GET /gateways/{id}/health."""

    gateway_id: UUID
    generated_at: datetime
    current: HealthSnapshotRead
    history: list[HealthSnapshotRead]       # oldest-first, last 24 hours
    history_window_hours: int = 24
