"""Add cost factor tables

Revision ID: 0b7c21741795
Revises: 0d312b10e473
Create Date: 2025-06-19 09:30:00.000000

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '0b7c21741795'
down_revision = '0d312b10e473'
branch_labels = None
depends_on = None


def upgrade():
    # Create cost_factors table
    op.create_table('cost_factors',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('category', sa.String(length=50), nullable=False),
        sa.Column('subcategory', sa.String(length=50), nullable=True),
        sa.Column('name', sa.String(length=100), nullable=False),
        sa.Column('factor_value', sa.Float(), nullable=False),
        sa.Column('calculation_formula', sa.String(length=200), nullable=True),
        sa.Column('percentage', sa.String(length=20), nullable=True),
        sa.Column('description', sa.String(length=500), nullable=True),
        sa.Column('is_active', sa.Boolean(), nullable=True),
        sa.Column('created_at', sa.DateTime(), nullable=True),
        sa.Column('updated_at', sa.DateTime(), nullable=True),
        sa.Column('updated_by', sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(['updated_by'], ['enterprise_users.id'], ),
        sa.PrimaryKeyConstraint('id')
    )
    
    # Create indexes for cost_factors table
    op.create_index('ix_cost_factor_category', 'cost_factors', ['category'], unique=False)
    op.create_index('ix_cost_factor_name', 'cost_factors', ['name'], unique=False)
    op.create_index('ix_cost_factor_active', 'cost_factors', ['is_active'], unique=False)
    
    # Create cost_factor_history table
    op.create_table('cost_factor_history',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('cost_factor_id', sa.Integer(), nullable=False),
        sa.Column('old_value', sa.Float(), nullable=True),
        sa.Column('new_value', sa.Float(), nullable=False),
        sa.Column('changed_by', sa.Integer(), nullable=False),
        sa.Column('changed_at', sa.DateTime(), nullable=True),
        sa.Column('change_reason', sa.String(length=500), nullable=True),
        sa.ForeignKeyConstraint(['changed_by'], ['enterprise_users.id'], ),
        sa.ForeignKeyConstraint(['cost_factor_id'], ['cost_factors.id'], ),
        sa.PrimaryKeyConstraint('id')
    )
    
    # Create indexes for cost_factor_history table
    op.create_index('ix_cost_factor_history_factor_id', 'cost_factor_history', ['cost_factor_id'], unique=False)
    op.create_index('ix_cost_factor_history_changed_at', 'cost_factor_history', ['changed_at'], unique=False)


def downgrade():
    # Drop indexes
    op.drop_index('ix_cost_factor_history_changed_at', table_name='cost_factor_history')
    op.drop_index('ix_cost_factor_history_factor_id', table_name='cost_factor_history')
    op.drop_index('ix_cost_factor_active', table_name='cost_factors')
    op.drop_index('ix_cost_factor_name', table_name='cost_factors')
    op.drop_index('ix_cost_factor_category', table_name='cost_factors')
    
    # Drop tables
    op.drop_table('cost_factor_history')
    op.drop_table('cost_factors') 