"""Add user_aircraft table for user-defined aircraft types

Revision ID: 004_user_aircraft
Revises: 003_public_booking
Create Date: 2026-03-23 12:00:00.000000

This migration adds a user_aircraft table to store aircraft types
that are manually entered by users when not found in the main aircraft database.

"""
from alembic import op
import sqlalchemy as sa

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


def upgrade() -> None:
    """
    Create user_aircraft table for storing user-defined aircraft types.
    """
    op.create_table('user_aircraft',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('registration', sa.String(length=25), nullable=False),
        sa.Column('type_code', sa.String(length=30), nullable=False),
        sa.Column('clean_reg', sa.String(length=25), nullable=False),
        sa.Column('created_by', sa.String(length=16), nullable=False),
        sa.Column('created_at', sa.DateTime(), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
        sa.Column('updated_at', sa.DateTime(), server_default=sa.text('CURRENT_TIMESTAMP'), onupdate=sa.text('CURRENT_TIMESTAMP'), nullable=False),
        sa.PrimaryKeyConstraint('id'),
        sa.UniqueConstraint('registration')
    )
    
    # Create indexes
    op.create_index('idx_user_aircraft_registration', 'user_aircraft', ['registration'])
    op.create_index('idx_user_aircraft_clean_reg', 'user_aircraft', ['clean_reg'])
    op.create_index('idx_user_aircraft_created_by', 'user_aircraft', ['created_by'])


def downgrade() -> None:
    """
    Drop user_aircraft table.
    """
    op.drop_index('idx_user_aircraft_created_by', table_name='user_aircraft')
    op.drop_index('idx_user_aircraft_clean_reg', table_name='user_aircraft')
    op.drop_index('idx_user_aircraft_registration', table_name='user_aircraft')
    op.drop_table('user_aircraft')