"""Rename processing_status values to user-meaningful lifecycle states.

Old: pending, transcribed, extracted, published
New: not_started, queued, processing, error, complete

Uses text column conversion to avoid PG enum ADD VALUE transaction restriction.

Revision ID: 008_rename_processing_status
Revises: 007_drop_review_columns
"""
from alembic import op
import sqlalchemy as sa

revision = "008_rename_processing_status"
down_revision = "007_drop_review_columns"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # 1. Drop server default (it references the old enum type)
    op.alter_column("source_videos", "processing_status", server_default=None)

    # 2. Convert column to text to break free of the old enum
    op.alter_column(
        "source_videos", "processing_status",
        type_=sa.Text(),
        existing_type=sa.Enum(name="processing_status"),
        postgresql_using="processing_status::text",
    )

    # 3. Drop old enum type
    op.execute("DROP TYPE IF EXISTS processing_status")

    # 4. Rename values in the text column
    op.execute("UPDATE source_videos SET processing_status = 'not_started' WHERE processing_status = 'pending'")
    op.execute("UPDATE source_videos SET processing_status = 'queued' WHERE processing_status = 'transcribed'")
    op.execute("UPDATE source_videos SET processing_status = 'processing' WHERE processing_status = 'extracted'")
    op.execute("UPDATE source_videos SET processing_status = 'complete' WHERE processing_status = 'published'")

    # 5. Create new enum type
    processing_status = sa.Enum(
        "not_started", "queued", "processing", "error", "complete",
        name="processing_status",
    )
    processing_status.create(op.get_bind(), checkfirst=True)

    # 6. Convert column back to enum with new default
    op.alter_column(
        "source_videos", "processing_status",
        type_=processing_status,
        existing_type=sa.Text(),
        postgresql_using="processing_status::processing_status",
        server_default="not_started",
    )


def downgrade() -> None:
    op.alter_column("source_videos", "processing_status", server_default=None)
    op.alter_column(
        "source_videos", "processing_status",
        type_=sa.Text(),
        existing_type=sa.Enum(name="processing_status"),
        postgresql_using="processing_status::text",
    )
    op.execute("DROP TYPE IF EXISTS processing_status")
    op.execute("UPDATE source_videos SET processing_status = 'pending' WHERE processing_status = 'not_started'")
    op.execute("UPDATE source_videos SET processing_status = 'transcribed' WHERE processing_status = 'queued'")
    op.execute("UPDATE source_videos SET processing_status = 'extracted' WHERE processing_status = 'processing'")
    op.execute("UPDATE source_videos SET processing_status = 'published' WHERE processing_status = 'complete'")
    old_enum = sa.Enum("pending", "transcribed", "extracted", "published", name="processing_status")
    old_enum.create(op.get_bind(), checkfirst=True)
    op.alter_column(
        "source_videos", "processing_status",
        type_=old_enum,
        existing_type=sa.Text(),
        postgresql_using="processing_status::processing_status",
        server_default="pending",
    )
