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


class ArrivalStatus(str, Enum):
    BOOKED_IN = "BOOKED_IN"
    LANDED = "LANDED"
    CANCELLED = "CANCELLED"


class SubmissionSource(str, Enum):
    ADMIN = "ADMIN"
    PUBLIC = "PUBLIC"


class ArrivalBase(BaseModel):
    registration: str
    type: Optional[str] = None
    callsign: Optional[str] = None
    pob: int
    in_from: str
    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('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


class ArrivalCreate(ArrivalBase):
    eta: Optional[datetime] = None


class ArrivalUpdate(BaseModel):
    registration: Optional[str] = None
    type: Optional[str] = None
    callsign: Optional[str] = None
    pob: Optional[int] = None
    in_from: Optional[str] = None
    notes: Optional[str] = None


class ArrivalStatusUpdate(BaseModel):
    status: ArrivalStatus
    timestamp: Optional[datetime] = None


class Arrival(ArrivalBase):
    id: int
    status: ArrivalStatus
    created_dt: datetime
    eta: Optional[datetime] = None
    landed_dt: Optional[datetime] = None
    created_by: Optional[str] = None
    updated_at: datetime
    submitted_via: Optional[SubmissionSource] = None
    pilot_email: Optional[str] = None

    class Config:
        from_attributes = True
