"""Add granular flight states and timestamps

Revision ID: 8adefaee847c
Revises: 004_user_aircraft
Create Date: 2026-03-24 09:09:00.944815

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '005_flight_states'
down_revision = '004_user_aircraft'
branch_labels = None
depends_on = None


def upgrade() -> None:
    # Add GROUND and LOCAL to local_flights status enum
    op.execute("ALTER TABLE local_flights MODIFY COLUMN status ENUM('BOOKED_OUT','GROUND','DEPARTED','LOCAL','CIRCUIT','LANDED','CANCELLED')")
    
    # Add timestamp columns to local_flights
    op.add_column('local_flights', sa.Column('contact_dt', sa.DateTime(), nullable=True))
    op.add_column('local_flights', sa.Column('takeoff_dt', sa.DateTime(), nullable=True))
    
    # Add GROUND and ARRIVED to arrivals status enum
    op.execute("ALTER TABLE arrivals MODIFY COLUMN status ENUM('BOOKED_IN','LANDED','GROUND','LOCAL','CIRCUIT','ARRIVED','CANCELLED')")
    
    # Add timestamp column to arrivals
    op.add_column('arrivals', sa.Column('arrived_dt', sa.DateTime(), nullable=True))
    
    # Add GROUND and LOCAL to departures status enum
    op.execute("ALTER TABLE departures MODIFY COLUMN status ENUM('BOOKED_OUT','GROUND','DEPARTED','LOCAL','CANCELLED')")
    
    # Add timestamp columns to departures
    op.add_column('departures', sa.Column('contact_dt', sa.DateTime(), nullable=True))
    op.add_column('departures', sa.Column('takeoff_dt', sa.DateTime(), nullable=True))
    
    # Add arrival_id column to circuits table to support circuit logging for arrivals
    op.add_column('circuits', sa.Column('arrival_id', sa.BigInteger(), nullable=True))
    op.create_foreign_key('fk_circuits_arrival_id', 'circuits', 'arrivals', ['arrival_id'], ['id'], ondelete='CASCADE')
    op.create_index('idx_circuit_arrival_id', 'circuits', ['arrival_id'])


def downgrade() -> None:
    # Remove arrival_id column from circuits table
    op.drop_constraint('fk_circuits_arrival_id', 'circuits', type_='foreignkey')
    op.drop_index('idx_circuit_arrival_id', table_name='circuits')
    op.drop_column('circuits', 'arrival_id')
    
    # Update departures with new status values to valid old values before modifying enum
    op.execute("UPDATE departures SET status = 'DEPARTED' WHERE status IN ('GROUND', 'LOCAL')")
    
    # Remove timestamp columns from departures
    op.drop_column('departures', 'takeoff_dt')
    op.drop_column('departures', 'contact_dt')
    
    # Remove GROUND and LOCAL from departures status enum
    op.execute("ALTER TABLE departures MODIFY COLUMN status ENUM('BOOKED_OUT','DEPARTED','CANCELLED')")
    
    # Update arrivals with new status values to valid old values before modifying enum
    op.execute("UPDATE arrivals SET status = 'LANDED' WHERE status IN ('GROUND', 'LOCAL', 'CIRCUIT', 'ARRIVED')")
    
    # Remove timestamp column from arrivals
    op.drop_column('arrivals', 'arrived_dt')
    
    # Remove GROUND and ARRIVED from arrivals status enum
    op.execute("ALTER TABLE arrivals MODIFY COLUMN status ENUM('BOOKED_IN','LANDED','CANCELLED')")
    
    # Update local_flights with new status values to valid old values before modifying enum
    op.execute("UPDATE local_flights SET status = 'DEPARTED' WHERE status IN ('GROUND', 'LOCAL', 'CIRCUIT')")
    
    # Remove timestamp columns from local_flights
    op.drop_column('local_flights', 'takeoff_dt')
    op.drop_column('local_flights', 'contact_dt')
    
    # Remove GROUND and LOCAL from local_flights status enum
    op.execute("ALTER TABLE local_flights MODIFY COLUMN status ENUM('BOOKED_OUT','DEPARTED','LANDED','CANCELLED')")
