"""Add per-article RAG push logs.

Revision ID: 0016_add_article_rag_push_log
Revises: 0015_add_attachment_source_url
Create Date: 2026-05-13
"""
from alembic import op
import sqlalchemy as sa


revision = "0016_add_article_rag_push_log"
down_revision = "0015_add_attachment_source_url"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "article_rag_push_log",
        sa.Column(
            "id",
            sa.BigInteger().with_variant(sa.Integer(), "sqlite"),
            primary_key=True,
            autoincrement=True,
        ),
        sa.Column(
            "article_id",
            sa.BigInteger(),
            sa.ForeignKey("article.id", ondelete="CASCADE"),
            nullable=False,
        ),
        sa.Column("source", sa.String(20), nullable=False),
        sa.Column("status", sa.String(20), nullable=False),
        sa.Column("file_count", sa.Integer(), nullable=False, server_default=sa.text("0")),
        sa.Column("duration_ms", sa.Integer(), nullable=True),
        sa.Column("started_at", sa.DateTime(), nullable=False),
        sa.Column("finished_at", sa.DateTime(), nullable=True),
        sa.Column("error_msg", sa.Text(), nullable=True),
        sa.Column("task_ids", sa.JSON(), nullable=True),
        sa.Column("rag_ingest_url", sa.Text(), nullable=True),
    )
    op.create_index(
        "ix_rag_push_log_article_started",
        "article_rag_push_log",
        ["article_id", "started_at"],
    )
    op.create_index(
        "ix_rag_push_log_status_started",
        "article_rag_push_log",
        ["status", "started_at"],
    )
    op.create_index(
        "ix_rag_push_log_source_started",
        "article_rag_push_log",
        ["source", "started_at"],
    )


def downgrade() -> None:
    op.drop_index("ix_rag_push_log_source_started", table_name="article_rag_push_log")
    op.drop_index("ix_rag_push_log_status_started", table_name="article_rag_push_log")
    op.drop_index("ix_rag_push_log_article_started", table_name="article_rag_push_log")
    op.drop_table("article_rag_push_log")
