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 ArrivalStatus(str, Enum):
    BOOKED_IN = "BOOKED_IN"
    LANDED = "LANDED"
    CANCELLED = "CANCELLED"


class Arrival(Base):
    __tablename__ = "arrivals"

    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)
    in_from = Column(String(4), nullable=False, index=True)
    status = Column(SQLEnum(ArrivalStatus), default=ArrivalStatus.BOOKED_IN, nullable=False, index=True)
    notes = Column(Text, nullable=True)
    created_dt = Column(DateTime, server_default=func.now(), nullable=False, index=True)
    eta = Column(DateTime, nullable=True, index=True)
    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, server_default=func.now(), onupdate=func.now(), nullable=False)
