"""add_validation_runs_table

Revision ID: 00002
Revises: 301bc59f6dfb
Create Date: 2025-11-17 16:30:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision: str = '00002'
down_revision: Union[str, None] = '301bc59f6dfb'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # Create validation_runs table
    op.create_table(
        'validation_runs',
        sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column('tenant_id', postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column('orchestration_run_id', postgresql.UUID(as_uuid=True), nullable=True),
        sa.Column('validation_start_date', sa.DateTime(timezone=True), nullable=False),
        sa.Column('validation_end_date', sa.DateTime(timezone=True), nullable=False),
        sa.Column('started_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('completed_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('duration_seconds', sa.Float(), nullable=True),
        sa.Column('status', sa.String(length=50), nullable=True),
        sa.Column('total_forecasts_evaluated', sa.Integer(), nullable=True),
        sa.Column('forecasts_with_actuals', sa.Integer(), nullable=True),
        sa.Column('forecasts_without_actuals', sa.Integer(), nullable=True),
        sa.Column('overall_mae', sa.Float(), nullable=True),
        sa.Column('overall_mape', sa.Float(), nullable=True),
        sa.Column('overall_rmse', sa.Float(), nullable=True),
        sa.Column('overall_r2_score', sa.Float(), nullable=True),
        sa.Column('overall_accuracy_percentage', sa.Float(), nullable=True),
        sa.Column('total_predicted_demand', sa.Float(), nullable=True),
        sa.Column('total_actual_demand', sa.Float(), nullable=True),
        sa.Column('metrics_by_product', postgresql.JSON(astext_type=sa.Text()), nullable=True),
        sa.Column('metrics_by_location', postgresql.JSON(astext_type=sa.Text()), nullable=True),
        sa.Column('metrics_records_created', sa.Integer(), nullable=True),
        sa.Column('error_message', sa.Text(), nullable=True),
        sa.Column('error_details', postgresql.JSON(astext_type=sa.Text()), nullable=True),
        sa.Column('triggered_by', sa.String(length=100), nullable=True),
        sa.Column('execution_mode', sa.String(length=50), nullable=True),
        sa.PrimaryKeyConstraint('id')
    )

    # Create indexes for validation_runs
    op.create_index(
        'ix_validation_runs_tenant_id',
        'validation_runs',
        ['tenant_id'],
        unique=False
    )
    op.create_index(
        'ix_validation_runs_tenant_created',
        'validation_runs',
        ['tenant_id', 'started_at'],
        unique=False
    )
    op.create_index(
        'ix_validation_runs_status',
        'validation_runs',
        ['status', 'started_at'],
        unique=False
    )
    op.create_index(
        'ix_validation_runs_orchestration',
        'validation_runs',
        ['orchestration_run_id'],
        unique=False
    )


def downgrade() -> None:
    # Drop indexes
    op.drop_index('ix_validation_runs_orchestration', table_name='validation_runs')
    op.drop_index('ix_validation_runs_status', table_name='validation_runs')
    op.drop_index('ix_validation_runs_tenant_created', table_name='validation_runs')
    op.drop_index('ix_validation_runs_tenant_id', table_name='validation_runs')

    # Drop table
    op.drop_table('validation_runs')
