"""Weather source schemas (CRUD)."""

from datetime import datetime
from typing import Dict, List, Literal, Optional

from pydantic import BaseModel, Field


class WeatherSourceCreate(BaseModel):
    """Request to create a weather source."""

    name: str = Field(description="Source name", min_length=1, max_length=100)
    provider: Literal["open_meteo"] = Field(default="open_meteo", description="Weather data provider")
    provider_config: Optional[Dict] = Field(None, description="Provider-specific configuration")
    latitude: float = Field(default=50.0, description="Geographic latitude (-90 to 90)", ge=-90.0, le=90.0)
    longitude: float = Field(default=0.0, description="Geographic longitude (-180 to 180)", ge=-180.0, le=180.0)
    update_interval: int = Field(default=600, description="API poll interval in seconds (60-3600)", ge=60, le=3600)
    description: Optional[str] = Field(None, description="Optional description", max_length=500)
    tags: List[str] = Field(default_factory=list, description="User-defined tags")


class WeatherSourceUpdate(BaseModel):
    """Request to update a weather source."""

    name: Optional[str] = Field(None, description="Source name", min_length=1, max_length=100)
    provider: Optional[Literal["open_meteo"]] = Field(None, description="Weather data provider")
    provider_config: Optional[Dict] = Field(None, description="Provider-specific configuration")
    latitude: Optional[float] = Field(None, description="Geographic latitude (-90 to 90)", ge=-90.0, le=90.0)
    longitude: Optional[float] = Field(None, description="Geographic longitude (-180 to 180)", ge=-180.0, le=180.0)
    update_interval: Optional[int] = Field(None, description="API poll interval in seconds (60-3600)", ge=60, le=3600)
    description: Optional[str] = Field(None, description="Optional description", max_length=500)
    tags: Optional[List[str]] = None


class WeatherSourceResponse(BaseModel):
    """Weather source response."""

    id: str = Field(description="Source ID")
    name: str = Field(description="Source name")
    provider: str = Field(description="Weather data provider")
    provider_config: Dict = Field(default_factory=dict, description="Provider-specific configuration")
    latitude: float = Field(description="Geographic latitude")
    longitude: float = Field(description="Geographic longitude")
    update_interval: int = Field(description="API poll interval in seconds")
    description: Optional[str] = Field(None, description="Description")
    tags: List[str] = Field(default_factory=list, description="User-defined tags")
    created_at: datetime = Field(description="Creation timestamp")
    updated_at: datetime = Field(description="Last update timestamp")


class WeatherSourceListResponse(BaseModel):
    """List of weather sources."""

    sources: List[WeatherSourceResponse] = Field(description="List of weather sources")
    count: int = Field(description="Number of sources")


class WeatherTestResponse(BaseModel):
    """Weather test/fetch result."""

    code: int = Field(description="WMO weather code")
    condition: str = Field(description="Human-readable condition name")
    temperature: float = Field(description="Temperature in Celsius")
    wind_speed: float = Field(description="Wind speed in km/h")
    cloud_cover: int = Field(description="Cloud cover percentage (0-100)")
