from pydantic import BaseModel, validator
from datetime import datetime
from typing import Optional
from enum import Enum


class OverflightStatus(str, Enum):
    ACTIVE = "ACTIVE"
    INACTIVE = "INACTIVE"
    CANCELLED = "CANCELLED"


class OverflightBase(BaseModel):
    registration: str  # Using registration as callsign
    pob: Optional[int] = None
    type: Optional[str] = None  # Aircraft type
    departure_airfield: Optional[str] = None
    destination_airfield: Optional[str] = None
    call_dt: datetime  # Time of initial call
    qsy_dt: Optional[datetime] = None  # Time of frequency change
    notes: Optional[str] = None

    @validator('registration')
    def validate_registration(cls, v):
        if not v or len(v.strip()) == 0:
            raise ValueError('Aircraft registration is required')
        return v.strip().upper()

    @validator('type')
    def validate_type(cls, v):
        if v and len(v.strip()) > 0:
            return v.strip()
        return v

    @validator('departure_airfield')
    def validate_departure_airfield(cls, v):
        if v and len(v.strip()) > 0:
            return v.strip().upper()
        return v

    @validator('destination_airfield')
    def validate_destination_airfield(cls, v):
        if v and len(v.strip()) > 0:
            return v.strip().upper()
        return v

    @validator('pob')
    def validate_pob(cls, v):
        if v is not None and v < 1:
            raise ValueError('Persons on board must be at least 1')
        return v


class OverflightCreate(OverflightBase):
    pass


class OverflightUpdate(BaseModel):
    callsign: Optional[str] = None
    pob: Optional[int] = None
    type: Optional[str] = None
    departure_airfield: Optional[str] = None
    destination_airfield: Optional[str] = None
    call_dt: Optional[datetime] = None
    qsy_dt: Optional[datetime] = None
    status: Optional[OverflightStatus] = None
    notes: Optional[str] = None

    @validator('type')
    def validate_type(cls, v):
        if v is not None and len(v.strip()) == 0:
            return None
        return v.strip() if v else v

    @validator('departure_airfield')
    def validate_departure_airfield(cls, v):
        if v is not None and len(v.strip()) == 0:
            return None
        return v.strip().upper() if v else v

    @validator('destination_airfield')
    def validate_destination_airfield(cls, v):
        if v is not None and len(v.strip()) == 0:
            return None
        return v.strip().upper() if v else v

    @validator('pob')
    def validate_pob(cls, v):
        if v is not None and v < 1:
            raise ValueError('Persons on board must be at least 1')
        return v


class OverflightStatusUpdate(BaseModel):
    status: OverflightStatus
    qsy_dt: Optional[datetime] = None


class Overflight(OverflightBase):
    id: int
    status: OverflightStatus
    created_dt: datetime
    created_by: Optional[str] = None
    updated_at: datetime

    class Config:
        from_attributes = True
