"""add_financial_audit_trail_table

Revision ID: d032f2351965
Revises: 6f7f8689fc98
Create Date: 2025-12-03 23:00:00.000000

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql


# revision identifiers, used by Alembic.
revision = 'd032f2351965'
down_revision = '6f7f8689fc98'
branch_labels = None
depends_on = None


def upgrade() -> None:
    # Check if table already exists
    from sqlalchemy import inspect
    bind = op.get_bind()
    inspector = inspect(bind)
    tables = inspector.get_table_names()
    
    if 'financial_audit_trail' not in tables:
        # Create financial_audit_trail table
        op.create_table(
            'financial_audit_trail',
            sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
            
            # Action details
            sa.Column('action_type', sa.Enum(
                'payment_created', 'payment_completed', 'payment_refunded', 
                'payment_failed', 'invoice_created', 'invoice_updated', 
                'invoice_paid', 'refund_processed', 'price_modified', 
                'discount_applied', 'promotion_applied',
                name='financialactiontype'
            ), nullable=False),
            sa.Column('action_description', sa.Text(), nullable=False),
            
            # Related entities
            sa.Column('payment_id', sa.Integer(), nullable=True),
            sa.Column('invoice_id', sa.Integer(), nullable=True),
            sa.Column('booking_id', sa.Integer(), nullable=True),
            
            # Financial details
            sa.Column('amount', sa.Numeric(precision=10, scale=2), nullable=True),
            sa.Column('previous_amount', sa.Numeric(precision=10, scale=2), nullable=True),
            sa.Column('currency', sa.String(length=3), nullable=True, server_default='USD'),
            
            # User information
            sa.Column('performed_by', sa.Integer(), nullable=False),
            sa.Column('performed_by_email', sa.String(length=255), nullable=True),
            
            # Additional context
            sa.Column('audit_metadata', sa.JSON(), nullable=True),
            sa.Column('notes', sa.Text(), nullable=True),
            
            # Timestamp
            sa.Column('created_at', sa.DateTime(), nullable=False),
            
            # Primary key
            sa.PrimaryKeyConstraint('id'),
            
            # Foreign keys
            sa.ForeignKeyConstraint(['payment_id'], ['payments.id'], name='fk_financial_audit_payment'),
            sa.ForeignKeyConstraint(['invoice_id'], ['invoices.id'], name='fk_financial_audit_invoice'),
            sa.ForeignKeyConstraint(['booking_id'], ['bookings.id'], name='fk_financial_audit_booking'),
            sa.ForeignKeyConstraint(['performed_by'], ['users.id'], name='fk_financial_audit_user')
        )
        
        # Create indexes
        op.create_index(op.f('ix_financial_audit_trail_id'), 'financial_audit_trail', ['id'], unique=False)
        op.create_index(op.f('ix_financial_audit_trail_action_type'), 'financial_audit_trail', ['action_type'], unique=False)
        op.create_index(op.f('ix_financial_audit_trail_payment_id'), 'financial_audit_trail', ['payment_id'], unique=False)
        op.create_index(op.f('ix_financial_audit_trail_invoice_id'), 'financial_audit_trail', ['invoice_id'], unique=False)
        op.create_index(op.f('ix_financial_audit_trail_booking_id'), 'financial_audit_trail', ['booking_id'], unique=False)
        op.create_index(op.f('ix_financial_audit_trail_performed_by'), 'financial_audit_trail', ['performed_by'], unique=False)
        op.create_index(op.f('ix_financial_audit_trail_created_at'), 'financial_audit_trail', ['created_at'], unique=False)
        
        # Create composite indexes
        op.create_index('idx_financial_audit_created', 'financial_audit_trail', ['created_at'], unique=False)
        op.create_index('idx_financial_audit_action', 'financial_audit_trail', ['action_type', 'created_at'], unique=False)
        op.create_index('idx_financial_audit_user', 'financial_audit_trail', ['performed_by', 'created_at'], unique=False)
        op.create_index('idx_financial_audit_booking', 'financial_audit_trail', ['booking_id', 'created_at'], unique=False)


def downgrade() -> None:
    # Drop indexes first
    op.drop_index('idx_financial_audit_booking', table_name='financial_audit_trail')
    op.drop_index('idx_financial_audit_user', table_name='financial_audit_trail')
    op.drop_index('idx_financial_audit_action', table_name='financial_audit_trail')
    op.drop_index('idx_financial_audit_created', table_name='financial_audit_trail')
    
    op.drop_index(op.f('ix_financial_audit_trail_created_at'), table_name='financial_audit_trail')
    op.drop_index(op.f('ix_financial_audit_trail_performed_by'), table_name='financial_audit_trail')
    op.drop_index(op.f('ix_financial_audit_trail_booking_id'), table_name='financial_audit_trail')
    op.drop_index(op.f('ix_financial_audit_trail_invoice_id'), table_name='financial_audit_trail')
    op.drop_index(op.f('ix_financial_audit_trail_payment_id'), table_name='financial_audit_trail')
    op.drop_index(op.f('ix_financial_audit_trail_action_type'), table_name='financial_audit_trail')
    op.drop_index(op.f('ix_financial_audit_trail_id'), table_name='financial_audit_trail')
    
    # Drop table
    op.drop_table('financial_audit_trail')
