"""Add local_flights table for tracking local flights

Revision ID: 002_local_flights
Revises: 001_initial_schema
Create Date: 2025-12-12 12:00:00.000000

This migration adds a new table for tracking local flights (circuits, local, departure)
that don't require PPR submissions. Also adds etd and renames booked_out_dt to created_dt,
and departure_dt to departed_dt for consistency.

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '002_local_flights'
down_revision = '001_initial_schema'
branch_labels = None
depends_on = None


def upgrade() -> None:
    """
    Create local_flights, departures, and arrivals tables.
    """
    
    op.create_table('local_flights',
        sa.Column('id', sa.BigInteger(), autoincrement=True, nullable=False),
        sa.Column('registration', sa.String(length=16), nullable=False),
        sa.Column('type', sa.String(length=32), nullable=True),
        sa.Column('callsign', sa.String(length=16), nullable=True),
        sa.Column('pob', sa.Integer(), nullable=False),
        sa.Column('flight_type', sa.Enum('LOCAL', 'CIRCUITS', 'DEPARTURE', name='localflighttype'), nullable=False),
        sa.Column('status', sa.Enum('BOOKED_OUT', 'DEPARTED', 'LANDED', 'CANCELLED', name='localflightstatus'), nullable=False, server_default='BOOKED_OUT'),
        sa.Column('notes', sa.Text(), nullable=True),
        sa.Column('created_dt', sa.DateTime(), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
        sa.Column('etd', sa.DateTime(), nullable=True),
        sa.Column('departed_dt', sa.DateTime(), nullable=True),
        sa.Column('landed_dt', sa.DateTime(), nullable=True),
        sa.Column('created_by', sa.String(length=16), nullable=True),
        sa.Column('updated_at', sa.DateTime(), server_default=sa.text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'), nullable=False),
        sa.PrimaryKeyConstraint('id'),
        mysql_engine='InnoDB',
        mysql_charset='utf8mb4',
        mysql_collate='utf8mb4_unicode_ci'
    )
    
    # Create indexes for local_flights
    op.create_index('idx_registration', 'local_flights', ['registration'])
    op.create_index('idx_flight_type', 'local_flights', ['flight_type'])
    op.create_index('idx_status', 'local_flights', ['status'])
    op.create_index('idx_created_dt', 'local_flights', ['created_dt'])
    op.create_index('idx_etd', 'local_flights', ['etd'])
    op.create_index('idx_created_by', 'local_flights', ['created_by'])
    
    # Create departures table for non-PPR departures to other airports
    op.create_table('departures',
        sa.Column('id', sa.BigInteger(), autoincrement=True, nullable=False),
        sa.Column('registration', sa.String(length=16), nullable=False),
        sa.Column('type', sa.String(length=32), nullable=True),
        sa.Column('callsign', sa.String(length=16), nullable=True),
        sa.Column('pob', sa.Integer(), nullable=False),
        sa.Column('out_to', sa.String(length=64), nullable=False),
        sa.Column('status', sa.Enum('BOOKED_OUT', 'DEPARTED', 'CANCELLED', name='departuresstatus'), nullable=False, server_default='BOOKED_OUT'),
        sa.Column('notes', sa.Text(), nullable=True),
        sa.Column('created_dt', sa.DateTime(), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
        sa.Column('etd', sa.DateTime(), nullable=True),
        sa.Column('departed_dt', sa.DateTime(), nullable=True),
        sa.Column('created_by', sa.String(length=16), nullable=True),
        sa.Column('updated_at', sa.DateTime(), server_default=sa.text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'), nullable=False),
        sa.PrimaryKeyConstraint('id'),
        mysql_engine='InnoDB',
        mysql_charset='utf8mb4',
        mysql_collate='utf8mb4_unicode_ci'
    )
    
    op.create_index('idx_dep_registration', 'departures', ['registration'])
    op.create_index('idx_dep_out_to', 'departures', ['out_to'])
    op.create_index('idx_dep_status', 'departures', ['status'])
    op.create_index('idx_dep_created_dt', 'departures', ['created_dt'])
    op.create_index('idx_dep_etd', 'departures', ['etd'])
    op.create_index('idx_dep_created_by', 'departures', ['created_by'])
    
    # Create arrivals table for non-PPR arrivals from elsewhere
    op.create_table('arrivals',
        sa.Column('id', sa.BigInteger(), autoincrement=True, nullable=False),
        sa.Column('registration', sa.String(length=16), nullable=False),
        sa.Column('type', sa.String(length=32), nullable=True),
        sa.Column('callsign', sa.String(length=16), nullable=True),
        sa.Column('pob', sa.Integer(), nullable=False),
        sa.Column('in_from', sa.String(length=64), nullable=False),
        sa.Column('status', sa.Enum('BOOKED_IN', 'LANDED', 'CANCELLED', name='arrivalsstatus'), nullable=False, server_default='BOOKED_IN'),
        sa.Column('notes', sa.Text(), nullable=True),
        sa.Column('booked_in_dt', sa.DateTime(), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
        sa.Column('landed_dt', sa.DateTime(), nullable=True),
        sa.Column('created_by', sa.String(length=16), nullable=True),
        sa.Column('updated_at', sa.DateTime(), server_default=sa.text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'), nullable=False),
        sa.PrimaryKeyConstraint('id'),
        mysql_engine='InnoDB',
        mysql_charset='utf8mb4',
        mysql_collate='utf8mb4_unicode_ci'
    )
    
    op.create_index('idx_arr_registration', 'arrivals', ['registration'])
    op.create_index('idx_arr_in_from', 'arrivals', ['in_from'])
    op.create_index('idx_arr_status', 'arrivals', ['status'])
    op.create_index('idx_arr_booked_in_dt', 'arrivals', ['booked_in_dt'])
    op.create_index('idx_arr_created_by', 'arrivals', ['created_by'])


def downgrade() -> None:
    """
    Drop the local_flights, departures, and arrivals tables.
    """
    op.drop_table('arrivals')
    op.drop_table('departures')
    op.drop_table('local_flights')
