"""bank statement analysis queue

Revision ID: 20260713_bank_stmt_queue
Revises: 20260711_reconcile_perm_doc_udin_aqmm
"""
from alembic import op
import sqlalchemy as sa

revision = "20260713_bank_stmt_queue"
down_revision = "20260711_reconcile_perm_doc_udin_aqmm"
branch_labels = None
depends_on = None


def upgrade():
    op.create_table(
        "bank_statement_analysis_jobs",
        sa.Column("id", sa.String(length=32), primary_key=True),
        sa.Column("tenant_id", sa.Integer(), sa.ForeignKey("tenants.id", ondelete="CASCADE"), nullable=True),
        sa.Column("branch_id", sa.Integer(), sa.ForeignKey("branches.id", ondelete="SET NULL"), nullable=True),
        sa.Column("user_id", sa.Integer(), sa.ForeignKey("users.id", ondelete="CASCADE"), nullable=False),
        sa.Column("role_bucket", sa.String(length=30), nullable=False),
        sa.Column("selected_bank", sa.String(length=40), nullable=False, server_default="auto"),
        sa.Column("financial_year", sa.String(length=20), nullable=True),
        sa.Column("customer_override", sa.String(length=255), nullable=True),
        sa.Column("account_override", sa.String(length=100), nullable=True),
        sa.Column("classification_enabled", sa.Boolean(), nullable=False, server_default=sa.true()),
        sa.Column("status", sa.String(length=20), nullable=False, server_default="queued"),
        sa.Column("progress_percent", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("file_count", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("input_files_json", sa.Text(), nullable=False),
        sa.Column("job_directory", sa.Text(), nullable=False),
        sa.Column("output_file", sa.Text(), nullable=True),
        sa.Column("summary_json", sa.Text(), nullable=True),
        sa.Column("error_message", sa.Text(), nullable=True),
        sa.Column("submitted_at_utc", sa.DateTime(timezone=True), nullable=False),
        sa.Column("started_at_utc", sa.DateTime(timezone=True), nullable=True),
        sa.Column("completed_at_utc", sa.DateTime(timezone=True), nullable=True),
        sa.Column("expires_at_utc", sa.DateTime(timezone=True), nullable=True),
    )
    op.create_index("ix_bank_stmt_jobs_tenant_id", "bank_statement_analysis_jobs", ["tenant_id"])
    op.create_index("ix_bank_stmt_jobs_branch_id", "bank_statement_analysis_jobs", ["branch_id"])
    op.create_index("ix_bank_stmt_jobs_user_id", "bank_statement_analysis_jobs", ["user_id"])
    op.create_index("ix_bank_stmt_jobs_status", "bank_statement_analysis_jobs", ["status"])
    op.create_index("ix_bank_stmt_jobs_submitted", "bank_statement_analysis_jobs", ["submitted_at_utc"])
    op.create_index("ix_bank_stmt_jobs_expires", "bank_statement_analysis_jobs", ["expires_at_utc"])


def downgrade():
    op.drop_table("bank_statement_analysis_jobs")
