"""
AOI (Area of Interest) Models
Pydantic models for AOI requests and responses
"""
from enum import Enum
from typing import Optional
from datetime import datetime
from pydantic import BaseModel, Field


class AOIStatus(str, Enum):
    """Status of an AOI processing job."""
    QUEUED = "queued"
    PROCESSING = "processing"
    COMPLETED = "completed"
    FAILED = "failed"


class AOIQuality(str, Enum):
    """Quality level for AOI bundle generation."""
    LOW = "low"
    MEDIUM = "medium"
    HIGH = "high"


class AOITheme(str, Enum):
    """Learning theme for AOI."""
    TOPOGRAPHIE = "topographie"
    LANDNUTZUNG = "landnutzung"
    ORIENTIERUNG = "orientierung"
    GEOLOGIE = "geologie"
    HYDROLOGIE = "hydrologie"
    VEGETATION = "vegetation"


class GeoJSONPolygon(BaseModel):
    """GeoJSON Polygon geometry."""
    type: str = Field("Polygon", const=True)
    coordinates: list[list[list[float]]] = Field(
        ...,
        description="Polygon coordinates as [[[lon, lat], ...]]",
    )


class AOIRequest(BaseModel):
    """Request model for creating an AOI."""
    polygon: dict = Field(
        ...,
        description="GeoJSON Polygon geometry",
        example={
            "type": "Polygon",
            "coordinates": [[[9.19, 47.71], [9.20, 47.71], [9.20, 47.70], [9.19, 47.70], [9.19, 47.71]]]
        },
    )
    theme: str = Field(
        "topographie",
        description="Learning theme for the AOI",
    )
    quality: str = Field(
        "medium",
        pattern="^(low|medium|high)$",
        description="Bundle quality level",
    )

    class Config:
        json_schema_extra = {
            "example": {
                "polygon": {
                    "type": "Polygon",
                    "coordinates": [[[9.1875, 47.7055], [9.1975, 47.7055], [9.1975, 47.7115], [9.1875, 47.7115], [9.1875, 47.7055]]]
                },
                "theme": "topographie",
                "quality": "medium",
            }
        }


class AOIResponse(BaseModel):
    """Response model for AOI operations."""
    aoi_id: str = Field(..., description="Unique AOI identifier")
    status: AOIStatus = Field(..., description="Current processing status")
    area_km2: float = Field(0, description="Area in square kilometers")
    estimated_size_mb: float = Field(0, description="Estimated bundle size in MB")
    message: Optional[str] = Field(None, description="Status message")
    download_url: Optional[str] = Field(None, description="Bundle download URL")
    manifest_url: Optional[str] = Field(None, description="Manifest URL")
    created_at: Optional[str] = Field(None, description="Creation timestamp")
    completed_at: Optional[str] = Field(None, description="Completion timestamp")

    class Config:
        json_schema_extra = {
            "example": {
                "aoi_id": "550e8400-e29b-41d4-a716-446655440000",
                "status": "completed",
                "area_km2": 0.45,
                "estimated_size_mb": 25.5,
                "message": "AOI processing complete",
                "download_url": "/api/v1/aoi/550e8400-e29b-41d4-a716-446655440000/bundle.zip",
                "manifest_url": "/api/v1/aoi/550e8400-e29b-41d4-a716-446655440000/manifest.json",
            }
        }


class AOIBounds(BaseModel):
    """Geographic bounding box."""
    west: float = Field(..., description="Western longitude")
    south: float = Field(..., description="Southern latitude")
    east: float = Field(..., description="Eastern longitude")
    north: float = Field(..., description="Northern latitude")


class AOICenter(BaseModel):
    """Geographic center point."""
    longitude: float
    latitude: float


class AOIManifest(BaseModel):
    """Unity bundle manifest for an AOI."""
    version: str = Field("1.0.0", description="Manifest version")
    aoi_id: str = Field(..., description="AOI identifier")
    created_at: str = Field(..., description="Creation timestamp")
    bounds: AOIBounds = Field(..., description="Geographic bounds")
    center: AOICenter = Field(..., description="Geographic center")
    area_km2: float = Field(..., description="Area in km²")
    theme: str = Field(..., description="Learning theme")
    quality: str = Field(..., description="Quality level")
    assets: dict = Field(..., description="Asset file references")
    unity: dict = Field(..., description="Unity-specific configuration")

    class Config:
        json_schema_extra = {
            "example": {
                "version": "1.0.0",
                "aoi_id": "550e8400-e29b-41d4-a716-446655440000",
                "created_at": "2024-01-15T12:00:00Z",
                "bounds": {
                    "west": 9.1875,
                    "south": 47.7055,
                    "east": 9.1975,
                    "north": 47.7115,
                },
                "center": {
                    "longitude": 9.1925,
                    "latitude": 47.7085,
                },
                "area_km2": 0.45,
                "theme": "topographie",
                "quality": "medium",
                "assets": {
                    "terrain": {"file": "terrain.heightmap.png", "config": "terrain.json"},
                    "osm_features": {"file": "osm_features.json"},
                    "learning_positions": {"file": "learning_positions.json"},
                    "attribution": {"file": "attribution.json"},
                },
                "unity": {
                    "coordinate_system": "Unity (Y-up, left-handed)",
                    "scale": 1.0,
                    "terrain_resolution": 256,
                },
            }
        }
