"""Add review_hours_status to contracts table

Revision ID: f9a8b2c3d4e5
Revises: e1588d3f77c7
Create Date: 2025-10-20 12:00:00.000000

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'f9a8b2c3d4e5'
down_revision = 'e1588d3f77c7'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    # Check if the contracts table exists before trying to inspect it
    conn = op.get_bind()
    inspector = sa.inspect(conn)
    
    # Only proceed if the contracts table exists
    if 'contracts' in inspector.get_table_names():
        columns = [col['name'] for col in inspector.get_columns('contracts')]
        
        # Add review_hours_status if it doesn't exist
        if 'review_hours_status' not in columns:
            with op.batch_alter_table('contracts', schema=None) as batch_op:
                batch_op.add_column(
                    sa.Column('review_hours_status', sa.String(length=50), 
                             nullable=False, server_default='pending')
                )
    # If table doesn't exist, skip this migration (fresh database)

    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    # Check if the contracts table exists before trying to modify it
    conn = op.get_bind()
    inspector = sa.inspect(conn)
    
    # Only proceed if the contracts table exists
    if 'contracts' in inspector.get_table_names():
        columns = [col['name'] for col in inspector.get_columns('contracts')]
        
        # Remove review_hours_status if it exists
        if 'review_hours_status' in columns:
            with op.batch_alter_table('contracts', schema=None) as batch_op:
                batch_op.drop_column('review_hours_status')
    
    # ### end Alembic commands ###
