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


class LocalFlightType(str, Enum):
    LOCAL = "LOCAL"
    CIRCUITS = "CIRCUITS"
    DEPARTURE = "DEPARTURE"


class PublicLocalFlightCreate(BaseModel):
    """Schema for public local flight booking"""
    registration: str
    type: Optional[str] = None  # Aircraft type - optional
    callsign: Optional[str] = None
    pob: int
    flight_type: LocalFlightType
    duration: Optional[int] = 45  # Duration in minutes, default 45
    etd: Optional[datetime] = None  # Estimated Time of Departure
    notes: Optional[str] = None
    pilot_email: Optional[str] = None  # Pilot's email for contact (optional)
    pilot_name: Optional[str] = None  # Pilot's name

    @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('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

    @validator('pilot_email', pre=True, always=False)
    def validate_pilot_email(cls, v):
        if v is None or v == '':
            return None
        return v.strip().lower()


class PublicCircuitCreate(BaseModel):
    """Schema for public circuit (touch and go) recording"""
    local_flight_id: int
    circuit_timestamp: datetime
    pilot_email: Optional[str] = None

    @validator('pilot_email', pre=True, always=False)
    def validate_pilot_email(cls, v):
        if v is None or v == '':
            return None
        return v.strip().lower()


class PublicDepartureCreate(BaseModel):
    """Schema for public departure booking"""
    registration: str
    type: Optional[str] = None
    callsign: Optional[str] = None
    pob: int
    out_to: str
    etd: Optional[datetime] = None  # Estimated Time of Departure
    notes: Optional[str] = None
    pilot_email: Optional[str] = None
    pilot_name: 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('out_to')
    def validate_out_to(cls, v):
        if not v or len(v.strip()) == 0:
            raise ValueError('Destination airport is required')
        return v.strip()

    @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

    @validator('pilot_email', pre=True, always=False)
    def validate_pilot_email(cls, v):
        if v is None or v == '':
            return None
        return v.strip().lower()


class PublicArrivalCreate(BaseModel):
    """Schema for public arrival booking"""
    registration: str
    type: Optional[str] = None
    callsign: Optional[str] = None
    pob: int
    in_from: str
    eta: Optional[datetime] = None
    notes: Optional[str] = None
    pilot_email: Optional[str] = None
    pilot_name: 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('in_from')
    def validate_in_from(cls, v):
        if not v or len(v.strip()) == 0:
            raise ValueError('Origin airport is required')
        return v.strip()

    @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

    @validator('pilot_email', pre=True, always=False)
    def validate_pilot_email(cls, v):
        if v is None or v == '':
            return None
        return v.strip().lower()
