"""
Monitor schemas for request/response validation
"""
from pydantic import BaseModel, Field
from typing import Optional
from datetime import datetime


class MonitorInfo(BaseModel):
    """Monitor information schema"""
    id: int = Field(..., description="Monitor ID (output channel number in GeViScope)")
    name: str = Field(..., description="Monitor name")
    description: Optional[str] = Field(None, description="Monitor description")
    status: str = Field(..., description="Monitor status (active, idle, offline, unknown)")
    current_camera_id: Optional[int] = Field(None, description="Currently displayed camera ID (None if no camera)")
    last_update: Optional[datetime] = Field(None, description="Last update timestamp")

    model_config = {
        "from_attributes": True,
        "json_schema_extra": {
            "examples": [
                {
                    "id": 1,
                    "name": "Control Room Monitor 1",
                    "description": "Main monitoring display",
                    "status": "active",
                    "current_camera_id": 5,
                    "last_update": "2025-12-09T10:30:00Z"
                }
            ]
        }
    }


class MonitorListResponse(BaseModel):
    """Response schema for monitor list endpoint"""
    monitors: list[MonitorInfo] = Field(..., description="List of monitors")
    total: int = Field(..., description="Total number of monitors")

    model_config = {
        "json_schema_extra": {
            "examples": [
                {
                    "monitors": [
                        {
                            "id": 1,
                            "name": "Control Room Monitor 1",
                            "description": "Main display",
                            "status": "active",
                            "current_camera_id": 5,
                            "last_update": "2025-12-09T10:30:00Z"
                        },
                        {
                            "id": 2,
                            "name": "Control Room Monitor 2",
                            "description": "Secondary display",
                            "status": "idle",
                            "current_camera_id": None,
                            "last_update": "2025-12-09T10:30:00Z"
                        }
                    ],
                    "total": 2
                }
            ]
        }
    }


class MonitorDetailResponse(BaseModel):
    """Response schema for single monitor detail"""
    id: int = Field(..., description="Monitor ID")
    name: str = Field(..., description="Monitor name")
    description: Optional[str] = Field(None, description="Monitor description")
    status: str = Field(..., description="Monitor status")
    current_camera_id: Optional[int] = Field(None, description="Currently displayed camera ID")
    current_camera_name: Optional[str] = Field(None, description="Currently displayed camera name")
    last_update: Optional[datetime] = Field(None, description="Last update timestamp")

    # Additional details
    channel_id: Optional[int] = Field(None, description="Physical channel ID")
    resolution: Optional[str] = Field(None, description="Monitor resolution (e.g., 1920x1080)")
    is_available: bool = Field(default=True, description="Whether monitor is available for cross-switching")

    model_config = {
        "from_attributes": True,
        "json_schema_extra": {
            "examples": [
                {
                    "id": 1,
                    "name": "Control Room Monitor 1",
                    "description": "Main monitoring display",
                    "status": "active",
                    "current_camera_id": 5,
                    "current_camera_name": "Entrance Camera",
                    "last_update": "2025-12-09T10:30:00Z",
                    "channel_id": 1,
                    "resolution": "1920x1080",
                    "is_available": True
                }
            ]
        }
    }


class MonitorStatusEnum:
    """Monitor status constants"""
    ACTIVE = "active"          # Monitor is displaying a camera
    IDLE = "idle"              # Monitor is on but not displaying anything
    OFFLINE = "offline"        # Monitor is not reachable
    UNKNOWN = "unknown"        # Monitor status cannot be determined
    ERROR = "error"            # Monitor has an error
    MAINTENANCE = "maintenance"  # Monitor is under maintenance
