"""SSE event schemas."""

from __future__ import annotations

from datetime import datetime
from enum import Enum
from typing import Any

from pydantic import BaseModel

from gps_denied.schemas import GPSPoint


class SSEEventType(str, Enum):
    """Server-Sent Event types emitted during flight processing."""

    FRAME_PROCESSED = "frame_processed"
    FRAME_REFINED = "frame_refined"
    SEARCH_EXPANDED = "search_expanded"
    USER_INPUT_NEEDED = "user_input_needed"
    PROCESSING_BLOCKED = "processing_blocked"
    FLIGHT_COMPLETED = "flight_completed"


class FrameProcessedEvent(BaseModel):
    """Payload for frame_processed / frame_refined events."""

    frame_id: int
    gps: GPSPoint
    altitude: float | None = None
    confidence: float
    heading: float | None = None
    timestamp: datetime


class SearchExpandedEvent(BaseModel):
    """Payload for search_expanded event."""

    frame_id: int
    grid_size: int
    message: str | None = None


class UserInputNeededEvent(BaseModel):
    """Payload for user_input_needed event."""

    frame_id: int
    reason: str
    consecutive_failures: int = 0


class FlightCompletedEvent(BaseModel):
    """Payload for flight_completed event."""

    frames_total: int
    frames_processed: int
    duration_seconds: float | None = None


class SSEMessage(BaseModel):
    """Generic SSE message wrapper."""

    event: SSEEventType
    data: dict[str, Any]
    id: str | None = None
    retry: int | None = None
