"""add_sales_data_updates_table

Revision ID: 00003
Revises: 00002
Create Date: 2025-11-17 17:00: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 = '00003'
down_revision: Union[str, None] = '00002'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # Create sales_data_updates table
    op.create_table(
        'sales_data_updates',
        sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column('tenant_id', postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column('update_date_start', sa.Date(), nullable=False),
        sa.Column('update_date_end', sa.Date(), nullable=False),
        sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('update_source', sa.String(length=100), nullable=True),
        sa.Column('records_affected', sa.Integer(), nullable=True),
        sa.Column('validation_status', sa.String(length=50), nullable=True),
        sa.Column('validation_run_id', postgresql.UUID(as_uuid=True), nullable=True),
        sa.Column('validated_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('validation_error', sa.String(length=500), nullable=True),
        sa.Column('requires_validation', sa.Boolean(), nullable=True),
        sa.Column('import_job_id', sa.String(length=255), nullable=True),
        sa.Column('notes', sa.String(length=500), nullable=True),
        sa.PrimaryKeyConstraint('id')
    )

    # Create indexes for sales_data_updates
    op.create_index(
        'ix_sales_data_updates_tenant_id',
        'sales_data_updates',
        ['tenant_id'],
        unique=False
    )
    op.create_index(
        'ix_sales_data_updates_update_date_start',
        'sales_data_updates',
        ['update_date_start'],
        unique=False
    )
    op.create_index(
        'ix_sales_data_updates_update_date_end',
        'sales_data_updates',
        ['update_date_end'],
        unique=False
    )
    op.create_index(
        'ix_sales_updates_tenant_status',
        'sales_data_updates',
        ['tenant_id', 'validation_status', 'created_at'],
        unique=False
    )
    op.create_index(
        'ix_sales_updates_date_range',
        'sales_data_updates',
        ['tenant_id', 'update_date_start', 'update_date_end'],
        unique=False
    )
    op.create_index(
        'ix_sales_updates_validation_status',
        'sales_data_updates',
        ['validation_status'],
        unique=False
    )


def downgrade() -> None:
    # Drop indexes
    op.drop_index('ix_sales_updates_validation_status', table_name='sales_data_updates')
    op.drop_index('ix_sales_updates_date_range', table_name='sales_data_updates')
    op.drop_index('ix_sales_updates_tenant_status', table_name='sales_data_updates')
    op.drop_index('ix_sales_data_updates_update_date_end', table_name='sales_data_updates')
    op.drop_index('ix_sales_data_updates_update_date_start', table_name='sales_data_updates')
    op.drop_index('ix_sales_data_updates_tenant_id', table_name='sales_data_updates')

    # Drop table
    op.drop_table('sales_data_updates')
