"""fc3i: task_run table

Revision ID: 0016
Revises: 0015
Create Date: 2026-05-24

Additive only. New table records every Celery task attempt via signal
handlers (backend.app.celery_signals). Status is plain String(16) not
Postgres ENUM (per feedback_check_existing_enums: ENUM columns hard-
fail at INSERT, String columns extend cleanly).

Composite indexes anticipate the three dashboard panes:
- (queue, started_at desc)   — per-lane recent activity
- (status, started_at desc)  — recent failures pane
- (task_name, started_at desc) — drill-down by task

Indexed columns get individual indexes via `index=True` on the model;
the composites below cover the multi-column lookups.
"""
from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

revision: str = "0016"
down_revision: Union[str, None] = "0015"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        "task_run",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("celery_task_id", sa.String(length=64), nullable=False),
        sa.Column("queue", sa.String(length=32), nullable=False),
        sa.Column("task_name", sa.String(length=128), nullable=False),
        sa.Column("target_id", sa.Integer(), nullable=True),
        sa.Column("started_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("finished_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("duration_ms", sa.Integer(), nullable=True),
        sa.Column(
            "status", sa.String(length=16), nullable=False,
            server_default="running",
        ),
        sa.Column("error_type", sa.String(length=128), nullable=True),
        sa.Column("error_message", sa.Text(), nullable=True),
        sa.Column("retry_count", sa.Integer(), nullable=True),
        sa.Column("worker_hostname", sa.String(length=128), nullable=True),
        sa.Column("args_summary", sa.String(length=255), nullable=True),
    )

    # Single-column indexes (matches Mapped[...].index=True on model).
    op.create_index("ix_task_run_celery_task_id", "task_run", ["celery_task_id"])
    op.create_index("ix_task_run_queue", "task_run", ["queue"])
    op.create_index("ix_task_run_task_name", "task_run", ["task_name"])
    op.create_index("ix_task_run_started_at", "task_run", ["started_at"])
    op.create_index("ix_task_run_finished_at", "task_run", ["finished_at"])
    op.create_index("ix_task_run_status", "task_run", ["status"])

    # Composite indexes for dashboard query patterns.
    op.create_index(
        "ix_task_run_queue_started",
        "task_run", ["queue", sa.text("started_at DESC")],
    )
    op.create_index(
        "ix_task_run_status_started",
        "task_run", ["status", sa.text("started_at DESC")],
    )
    op.create_index(
        "ix_task_run_name_started",
        "task_run", ["task_name", sa.text("started_at DESC")],
    )


def downgrade() -> None:
    op.drop_index("ix_task_run_name_started", table_name="task_run")
    op.drop_index("ix_task_run_status_started", table_name="task_run")
    op.drop_index("ix_task_run_queue_started", table_name="task_run")
    op.drop_index("ix_task_run_status", table_name="task_run")
    op.drop_index("ix_task_run_finished_at", table_name="task_run")
    op.drop_index("ix_task_run_started_at", table_name="task_run")
    op.drop_index("ix_task_run_task_name", table_name="task_run")
    op.drop_index("ix_task_run_queue", table_name="task_run")
    op.drop_index("ix_task_run_celery_task_id", table_name="task_run")
    op.drop_table("task_run")
