"""
Cross-switch schemas for request/response validation
"""
from pydantic import BaseModel, Field, field_validator
from typing import Optional, List
from datetime import datetime


class CrossSwitchRequest(BaseModel):
    """Request schema for executing cross-switch"""
    camera_id: int = Field(..., gt=0, description="Camera ID (must be positive)")
    monitor_id: int = Field(..., gt=0, description="Monitor ID (must be positive)")
    mode: int = Field(default=0, ge=0, description="Cross-switch mode (default: 0=normal)")

    @field_validator('camera_id', 'monitor_id')
    @classmethod
    def validate_positive_id(cls, v: int) -> int:
        """Ensure IDs are positive"""
        if v <= 0:
            raise ValueError('ID must be positive')
        return v

    model_config = {
        "json_schema_extra": {
            "examples": [
                {
                    "camera_id": 1,
                    "monitor_id": 1,
                    "mode": 0
                }
            ]
        }
    }


class ClearMonitorRequest(BaseModel):
    """Request schema for clearing a monitor"""
    monitor_id: int = Field(..., gt=0, description="Monitor ID to clear (must be positive)")

    @field_validator('monitor_id')
    @classmethod
    def validate_positive_id(cls, v: int) -> int:
        """Ensure monitor ID is positive"""
        if v <= 0:
            raise ValueError('Monitor ID must be positive')
        return v

    model_config = {
        "json_schema_extra": {
            "examples": [
                {
                    "monitor_id": 1
                }
            ]
        }
    }


class RouteInfo(BaseModel):
    """Route information schema"""
    id: str = Field(..., description="Route UUID")
    camera_id: int = Field(..., description="Camera ID")
    monitor_id: int = Field(..., description="Monitor ID")
    mode: int = Field(default=0, description="Cross-switch mode")
    executed_at: datetime = Field(..., description="When route was executed")
    executed_by: Optional[str] = Field(None, description="User ID who executed the route")
    executed_by_username: Optional[str] = Field(None, description="Username who executed the route")
    is_active: bool = Field(..., description="Whether route is currently active")
    camera_name: Optional[str] = Field(None, description="Camera name")
    monitor_name: Optional[str] = Field(None, description="Monitor name")

    model_config = {
        "from_attributes": True,
        "json_schema_extra": {
            "examples": [
                {
                    "id": "550e8400-e29b-41d4-a716-446655440000",
                    "camera_id": 1,
                    "monitor_id": 1,
                    "mode": 0,
                    "executed_at": "2025-12-09T10:30:00Z",
                    "executed_by": "550e8400-e29b-41d4-a716-446655440001",
                    "executed_by_username": "operator",
                    "is_active": True,
                    "camera_name": "Entrance Camera",
                    "monitor_name": "Control Room Monitor 1"
                }
            ]
        }
    }


class CrossSwitchResponse(BaseModel):
    """Response schema for successful cross-switch execution"""
    success: bool = Field(..., description="Whether operation succeeded")
    message: str = Field(..., description="Success message")
    route: RouteInfo = Field(..., description="Route information")

    model_config = {
        "json_schema_extra": {
            "examples": [
                {
                    "success": True,
                    "message": "Successfully switched camera 1 to monitor 1",
                    "route": {
                        "id": "550e8400-e29b-41d4-a716-446655440000",
                        "camera_id": 1,
                        "monitor_id": 1,
                        "mode": 0,
                        "executed_at": "2025-12-09T10:30:00Z",
                        "executed_by": "550e8400-e29b-41d4-a716-446655440001",
                        "executed_by_username": "operator",
                        "is_active": True,
                        "camera_name": "Entrance Camera",
                        "monitor_name": "Control Room Monitor 1"
                    }
                }
            ]
        }
    }


class ClearMonitorResponse(BaseModel):
    """Response schema for successful clear monitor operation"""
    success: bool = Field(..., description="Whether operation succeeded")
    message: str = Field(..., description="Success message")
    monitor_id: int = Field(..., description="Monitor ID that was cleared")

    model_config = {
        "json_schema_extra": {
            "examples": [
                {
                    "success": True,
                    "message": "Successfully cleared monitor 1",
                    "monitor_id": 1
                }
            ]
        }
    }


class RoutingStateResponse(BaseModel):
    """Response schema for routing state query"""
    routes: List[RouteInfo] = Field(..., description="List of active routes")
    total: int = Field(..., description="Total number of active routes")

    model_config = {
        "json_schema_extra": {
            "examples": [
                {
                    "routes": [
                        {
                            "id": "550e8400-e29b-41d4-a716-446655440000",
                            "camera_id": 1,
                            "monitor_id": 1,
                            "mode": 0,
                            "executed_at": "2025-12-09T10:30:00Z",
                            "executed_by": "550e8400-e29b-41d4-a716-446655440001",
                            "executed_by_username": "operator",
                            "is_active": True,
                            "camera_name": "Entrance Camera",
                            "monitor_name": "Control Room Monitor 1"
                        }
                    ],
                    "total": 1
                }
            ]
        }
    }


class RouteHistoryResponse(BaseModel):
    """Response schema for routing history query"""
    history: List[RouteInfo] = Field(..., description="List of historical routes")
    total: int = Field(..., description="Total number of historical records")
    limit: int = Field(..., description="Pagination limit")
    offset: int = Field(..., description="Pagination offset")

    model_config = {
        "json_schema_extra": {
            "examples": [
                {
                    "history": [
                        {
                            "id": "550e8400-e29b-41d4-a716-446655440000",
                            "camera_id": 1,
                            "monitor_id": 1,
                            "mode": 0,
                            "executed_at": "2025-12-09T10:30:00Z",
                            "executed_by": "550e8400-e29b-41d4-a716-446655440001",
                            "executed_by_username": "operator",
                            "is_active": False,
                            "camera_name": "Entrance Camera",
                            "monitor_name": "Control Room Monitor 1"
                        }
                    ],
                    "total": 50,
                    "limit": 10,
                    "offset": 0
                }
            ]
        }
    }
