from sqlalchemy import Column, Integer, String, DateTime, Text, Enum as SQLEnum, BigInteger
from sqlalchemy.sql import func
from enum import Enum
from app.db.session import Base


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


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


class LocalFlightStatus(str, Enum):
    BOOKED_OUT = "BOOKED_OUT"
    DEPARTED = "DEPARTED"
    LANDED = "LANDED"
    CANCELLED = "CANCELLED"


class LocalFlight(Base):
    __tablename__ = "local_flights"
    
    id = Column(BigInteger, primary_key=True, autoincrement=True)
    registration = Column(String(16), nullable=False, index=True)
    type = Column(String(32), nullable=False)  # Aircraft type
    callsign = Column(String(16), nullable=True)
    pob = Column(Integer, nullable=False)  # Persons on board
    flight_type = Column(SQLEnum(LocalFlightType), nullable=False, index=True)
    status = Column(SQLEnum(LocalFlightStatus), nullable=False, default=LocalFlightStatus.BOOKED_OUT, index=True)
    duration = Column(Integer, nullable=True)  # Duration in minutes
    circuits = Column(Integer, nullable=True, default=0)  # Actual number of circuits completed
    notes = Column(Text, nullable=True)
    created_dt = Column(DateTime, nullable=False, server_default=func.current_timestamp(), index=True)
    etd = Column(DateTime, nullable=True, index=True)  # Estimated Time of Departure
    departed_dt = Column(DateTime, nullable=True)  # Actual takeoff time
    landed_dt = Column(DateTime, nullable=True)
    created_by = Column(String(16), nullable=True, index=True)
    submitted_via = Column(SQLEnum(SubmissionSource), nullable=False, default=SubmissionSource.ADMIN, index=True)
    pilot_email = Column(String(128), nullable=True)  # For public submissions
    updated_at = Column(DateTime, nullable=False, server_default=func.current_timestamp(), onupdate=func.current_timestamp())
