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 PPRStatus(str, Enum):
    NEW = "NEW"
    CONFIRMED = "CONFIRMED"
    CANCELED = "CANCELED"
    LANDED = "LANDED"
    DELETED = "DELETED"
    DEPARTED = "DEPARTED"


class UserRole(str, Enum):
    ADMINISTRATOR = "ADMINISTRATOR"
    OPERATOR = "OPERATOR"
    READ_ONLY = "READ_ONLY"


class PPRRecord(Base):
    __tablename__ = "submitted"
    
    id = Column(BigInteger, primary_key=True, autoincrement=True)
    status = Column(SQLEnum(PPRStatus), nullable=False, default=PPRStatus.NEW, index=True)
    ac_reg = Column(String(16), nullable=False, index=True)
    ac_type = Column(String(32), nullable=False)
    ac_call = Column(String(16), nullable=True)
    captain = Column(String(64), nullable=False)
    fuel = Column(String(16), nullable=True)
    in_from = Column(String(64), nullable=False)
    eta = Column(DateTime, nullable=False, index=True)
    pob_in = Column(Integer, nullable=False)
    out_to = Column(String(64), nullable=True)
    etd = Column(DateTime, nullable=True, index=True)
    pob_out = Column(Integer, nullable=True)
    email = Column(String(128), nullable=True)
    phone = Column(String(16), nullable=True)
    notes = Column(Text, nullable=True)
    landed_dt = Column(DateTime, nullable=True)
    departed_dt = Column(DateTime, nullable=True)
    created_by = Column(String(16), nullable=True, index=True)
    submitted_dt = Column(DateTime, nullable=False, server_default=func.current_timestamp(), index=True)
    public_token = Column(String(128), nullable=True, unique=True, index=True)
    updated_at = Column(DateTime, nullable=False, server_default=func.current_timestamp(), onupdate=func.current_timestamp())


class User(Base):
    __tablename__ = "users"
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    username = Column(String(50), nullable=False, unique=True, index=True)
    password = Column(String(255), nullable=False)
    role = Column(SQLEnum(UserRole), nullable=False, default=UserRole.READ_ONLY)
    email = Column(String(128), nullable=True)
    full_name = Column(String(100), nullable=True)
    is_active = Column(Integer, nullable=False, default=1)  # Using Integer for BOOLEAN
    created_at = Column(DateTime, nullable=False, server_default=func.current_timestamp())
    updated_at = Column(DateTime, nullable=False, server_default=func.current_timestamp(), onupdate=func.current_timestamp())


class Airport(Base):
    __tablename__ = "airports"
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    icao = Column(String(4), nullable=False, unique=True, index=True)
    iata = Column(String(3), nullable=True, index=True)
    name = Column(String(255), nullable=False)
    country = Column(String(100), nullable=False)
    city = Column(String(100), nullable=True)
    timezone = Column(String(50), nullable=True)
    latitude = Column(Text, nullable=True)
    longitude = Column(Text, nullable=True)
    elevation = Column(Integer, nullable=True)
    created_at = Column(DateTime, nullable=False, server_default=func.current_timestamp())


class Aircraft(Base):
    __tablename__ = "aircraft"
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    icao24 = Column(String(6), nullable=True, index=True)
    registration = Column(String(25), nullable=True, index=True)
    manufacturer_icao = Column(String(50), nullable=True)
    type_code = Column(String(30), nullable=True, index=True)
    manufacturer_name = Column(String(255), nullable=True)
    model = Column(String(255), nullable=True)
    clean_reg = Column(String(25), nullable=True, index=True)
    created_at = Column(DateTime, nullable=False, server_default=func.current_timestamp())
    updated_at = Column(DateTime, nullable=False, server_default=func.current_timestamp(), onupdate=func.current_timestamp())


class UserAircraft(Base):
    __tablename__ = "user_aircraft"
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    registration = Column(String(25), nullable=False, unique=True, index=True)
    type_code = Column(String(30), nullable=False)
    clean_reg = Column(String(25), nullable=False, index=True)
    created_by = Column(String(16), nullable=False, index=True)
    created_at = Column(DateTime, nullable=False, server_default=func.current_timestamp())
    updated_at = Column(DateTime, nullable=False, server_default=func.current_timestamp(), onupdate=func.current_timestamp())