"""Add classification_data JSONB column to source_videos and stage_rerun trigger.

Persists stage 4 classification data in PostgreSQL alongside Redis cache,
eliminating the 24-hour TTL data loss risk. Also adds the 'stage_rerun'
trigger value for single-stage re-run support.

Revision ID: 011_cls_cache_rerun
Revises: 010_add_pipeline_runs
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import JSONB

revision = "011_cls_cache_rerun"
down_revision = "010_add_pipeline_runs"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # Add classification_data column to source_videos
    op.add_column(
        "source_videos",
        sa.Column("classification_data", JSONB, nullable=True),
    )

    # Add 'stage_rerun' to the pipeline_run_trigger enum
    # PostgreSQL enums require ALTER TYPE to add values
    op.execute("ALTER TYPE pipeline_run_trigger ADD VALUE IF NOT EXISTS 'stage_rerun'")


def downgrade() -> None:
    op.drop_column("source_videos", "classification_data")
    # Note: PostgreSQL does not support removing values from enums.
    # The 'stage_rerun' value will remain but be unused after downgrade.
