from pydantic import BaseModel, Field, field_validator, ConfigDict
from typing import Optional, List
from datetime import datetime
from enum import Enum


class AircraftStatus(str, Enum):
    ACTIVE = "Active"
    MAINTENANCE = "Maintenance"
    RETIRED = "Retired"


class SeatClassType(str, Enum):
    ECONOMY = "Economy"
    PREMIUM_ECONOMY = "Premium Economy"
    BUSINESS = "Business"
    FIRST_CLASS = "First Class"


class AircraftBase(BaseModel):
    registration_number: str = Field(..., min_length=1, max_length=50)
    model: str = Field(..., min_length=1, max_length=100)
    manufacturer: str = Field(..., min_length=1, max_length=100)
    total_seats: int = Field(..., gt=0)
    year_manufactured: Optional[int] = None
    status: AircraftStatus = AircraftStatus.ACTIVE

    @field_validator("total_seats")
    @classmethod
    def validate_total_seats(cls, v):
        if v <= 0:
            raise ValueError("total_seats must be greater than 0")
        return v

    @field_validator("year_manufactured")
    @classmethod
    def validate_year_manufactured(cls, v):
        if v is not None and v <= 1900:
            raise ValueError("year_manufactured must be greater than 1900")
        return v


class AircraftCreate(AircraftBase):
    pass


class AircraftUpdate(BaseModel):
    registration_number: Optional[str] = Field(None, min_length=1, max_length=50)
    model: Optional[str] = Field(None, min_length=1, max_length=100)
    manufacturer: Optional[str] = Field(None, min_length=1, max_length=100)
    total_seats: Optional[int] = Field(None, gt=0)
    year_manufactured: Optional[int] = None
    status: Optional[AircraftStatus] = None

    @field_validator("total_seats")
    @classmethod
    def validate_total_seats(cls, v):
        if v is not None and v <= 0:
            raise ValueError("total_seats must be greater than 0")
        return v

    @field_validator("year_manufactured")
    @classmethod
    def validate_year_manufactured(cls, v):
        if v is not None and v <= 1900:
            raise ValueError("year_manufactured must be greater than 1900")
        return v


class AircraftResponse(AircraftBase):
    id: str
    created_at: datetime
    updated_at: datetime

    model_config = ConfigDict(from_attributes=True)


class SeatBase(BaseModel):
    aircraft_id: str
    seat_number: str = Field(..., min_length=1, max_length=10)
    class_type: SeatClassType
    is_window: bool = False
    is_aisle: bool = False
    is_exit_row: bool = False


class SeatCreate(SeatBase):
    pass


class SeatUpdate(BaseModel):
    aircraft_id: Optional[str] = None
    seat_number: Optional[str] = Field(None, min_length=1, max_length=10)
    class_type: Optional[SeatClassType] = None
    is_window: Optional[bool] = None
    is_aisle: Optional[bool] = None
    is_exit_row: Optional[bool] = None


class SeatResponse(SeatBase):
    id: str
    created_at: datetime
    updated_at: datetime

    model_config = ConfigDict(from_attributes=True)


class SeatDetailResponse(BaseModel):
    seat_number: str
    class_type: str
    is_window: bool
    is_aisle: bool

    model_config = ConfigDict(from_attributes=True)


class FlightSummaryResponse(BaseModel):
    flight_number: str
    scheduled_departure: datetime
    status: str

    model_config = ConfigDict(from_attributes=True)


class AircraftDetailResponse(BaseModel):
    id: str
    registration_number: str
    model: str
    manufacturer: str
    total_seats: int
    year_manufactured: Optional[int]
    status: str
    created_at: datetime
    updated_at: datetime
    seats: List[SeatDetailResponse]
    flights: List[FlightSummaryResponse]

    model_config = ConfigDict(from_attributes=True)