"""add persistent attachment association jobs

Revision ID: 20260716_0006
Revises: 20260714_0005
Create Date: 2026-07-16 09:55:00
"""

from collections.abc import Sequence

import sqlalchemy as sa

from alembic import op

revision: str = "20260716_0006"
down_revision: str | None = "20260714_0005"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    op.create_table(
        "attachment_association_jobs",
        sa.Column("id", sa.String(length=80), nullable=False),
        sa.Column("tenant_id", sa.String(length=64), nullable=False),
        sa.Column("owner_username", sa.String(length=255), nullable=False),
        sa.Column("owner_name", sa.String(length=255), server_default="", nullable=False),
        sa.Column("owner_context_json", sa.JSON(), nullable=False),
        sa.Column("dedupe_key", sa.String(length=64), nullable=False),
        sa.Column("generation", sa.Integer(), server_default="1", nullable=False),
        sa.Column("receipt_ids_json", sa.JSON(), nullable=False),
        sa.Column("prompt", sa.Text(), server_default="", nullable=False),
        sa.Column("conversation_id", sa.String(length=120), server_default="", nullable=False),
        sa.Column("status", sa.String(length=20), server_default="queued", nullable=False),
        sa.Column("message", sa.Text(), server_default="", nullable=False),
        sa.Column("claim_id", sa.String(length=36), server_default="", nullable=False),
        sa.Column("claim_no", sa.String(length=80), server_default="", nullable=False),
        sa.Column("uploaded_count", sa.Integer(), server_default="0", nullable=False),
        sa.Column("skipped_count", sa.Integer(), server_default="0", nullable=False),
        sa.Column("resolution", sa.String(length=40), server_default="pending", nullable=False),
        sa.Column(
            "requires_confirmation",
            sa.Boolean(),
            server_default=sa.false(),
            nullable=False,
        ),
        sa.Column("expense_case_id", sa.String(length=36), server_default="", nullable=False),
        sa.Column("application_claim_id", sa.String(length=36), server_default="", nullable=False),
        sa.Column("application_claim_no", sa.String(length=80), server_default="", nullable=False),
        sa.Column("confidence", sa.String(length=20), server_default="", nullable=False),
        sa.Column("confidence_score", sa.Float(), server_default="0", nullable=False),
        sa.Column("match_reasons_json", sa.JSON(), nullable=False),
        sa.Column("exceptions_json", sa.JSON(), nullable=False),
        sa.Column("missing_fields_json", sa.JSON(), nullable=False),
        sa.Column("risk_items_json", sa.JSON(), nullable=False),
        sa.Column("candidates_json", sa.JSON(), nullable=False),
        sa.Column("draft_payload_json", sa.JSON(), nullable=True),
        sa.Column("error", sa.Text(), server_default="", nullable=False),
        sa.Column("attempt_count", sa.Integer(), server_default="0", nullable=False),
        sa.Column("lease_expires_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            server_default=sa.func.now(),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.DateTime(timezone=True),
            server_default=sa.func.now(),
            nullable=False,
        ),
        sa.CheckConstraint(
            "status IN ('queued', 'running', 'succeeded', 'failed')",
            name="ck_attachment_association_jobs_status",
        ),
        sa.CheckConstraint(
            "attempt_count >= 0",
            name="ck_attachment_association_jobs_attempt_count",
        ),
        sa.CheckConstraint(
            "generation >= 1",
            name="ck_attachment_association_jobs_generation",
        ),
        sa.CheckConstraint(
            "status != 'running' OR lease_expires_at IS NOT NULL",
            name="ck_attachment_association_jobs_running_lease",
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "tenant_id",
            "owner_username",
            "dedupe_key",
            "generation",
            name="uq_attachment_association_jobs_owner_dedupe",
        ),
    )
    op.create_index(
        "ix_attachment_association_jobs_owner_time",
        "attachment_association_jobs",
        ["tenant_id", "owner_username", "created_at"],
    )
    op.create_index(
        "ix_attachment_association_jobs_status_lease",
        "attachment_association_jobs",
        ["status", "lease_expires_at"],
    )


def downgrade() -> None:
    op.drop_index(
        "ix_attachment_association_jobs_status_lease",
        table_name="attachment_association_jobs",
    )
    op.drop_index(
        "ix_attachment_association_jobs_owner_time",
        table_name="attachment_association_jobs",
    )
    op.drop_table("attachment_association_jobs")
