from sqlalchemy import Column, BigInteger, String, Integer, Text, DateTime, Enum as SQLEnum, func
from sqlalchemy.ext.declarative import declarative_base
from enum import Enum
from datetime import datetime

Base = declarative_base()


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


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


class Departure(Base):
    __tablename__ = "departures"

    id = Column(BigInteger, primary_key=True, autoincrement=True)
    registration = Column(String(16), nullable=False, index=True)
    type = Column(String(32), nullable=True)
    callsign = Column(String(16), nullable=True)
    pob = Column(Integer, nullable=False)
    out_to = Column(String(64), nullable=False, index=True)
    status = Column(SQLEnum(DepartureStatus), default=DepartureStatus.BOOKED_OUT, nullable=False, index=True)
    notes = Column(Text, nullable=True)
    created_dt = Column(DateTime, server_default=func.now(), nullable=False, index=True)
    etd = Column(DateTime, nullable=True, index=True)  # Estimated Time of Departure
    departed_dt = Column(DateTime, nullable=True)  # Actual departure time
    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, server_default=func.now(), onupdate=func.now(), nullable=False)
