"""Add body_sections_format column and technique_page_videos association table.

Supports multi-source technique pages: tracks which source videos contributed
to a technique page, and marks the body_sections format version for future
structured section layouts.

Revision ID: 012_multi_source_fmt
Revises: 011_cls_cache_rerun
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import UUID

revision = "012_multi_source_fmt"
down_revision = "011_cls_cache_rerun"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # Add body_sections_format to technique_pages with default for existing rows
    op.add_column(
        "technique_pages",
        sa.Column(
            "body_sections_format",
            sa.String(20),
            nullable=False,
            server_default="v1",
        ),
    )

    # Create technique_page_videos association table
    op.create_table(
        "technique_page_videos",
        sa.Column("id", UUID(as_uuid=True), primary_key=True, server_default=sa.func.gen_random_uuid()),
        sa.Column(
            "technique_page_id",
            UUID(as_uuid=True),
            sa.ForeignKey("technique_pages.id", ondelete="CASCADE"),
            nullable=False,
        ),
        sa.Column(
            "source_video_id",
            UUID(as_uuid=True),
            sa.ForeignKey("source_videos.id", ondelete="CASCADE"),
            nullable=False,
        ),
        sa.Column("added_at", sa.TIMESTAMP(), server_default=sa.func.now(), nullable=False),
        sa.UniqueConstraint("technique_page_id", "source_video_id", name="uq_page_video"),
    )


def downgrade() -> None:
    op.drop_table("technique_page_videos")
    op.drop_column("technique_pages", "body_sections_format")
