"""playback_sources table for tube/aggregator video links

Revision ID: 0002_playback_sources
Revises: 0001_initial
Create Date: 2026-05-02

"""
from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

revision: str = "0002_playback_sources"
down_revision: str | None = "0001_initial"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    op.create_table(
        "playback_sources",
        sa.Column(
            "id",
            postgresql.UUID(as_uuid=True),
            primary_key=True,
            server_default=sa.text("gen_random_uuid()"),
        ),
        sa.Column(
            "scene_id",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("scenes.id", ondelete="CASCADE"),
            nullable=False,
        ),
        sa.Column("origin", sa.String(64), nullable=False),
        sa.Column("page_url", sa.String(2048), nullable=False),
        sa.Column("embed_url", sa.String(2048)),
        sa.Column("stream_url", sa.String(2048)),
        sa.Column("quality", sa.String(16)),
        sa.Column("duration_sec", sa.Integer),
        sa.Column("thumbnail_url", sa.String(2048)),
        sa.Column(
            "last_seen_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("NOW()"),
            nullable=False,
        ),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("NOW()"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("NOW()"),
            nullable=False,
        ),
        sa.UniqueConstraint("origin", "page_url", name="uq_playback_sources_origin_page_url"),
    )
    op.create_index("ix_playback_sources_scene_id", "playback_sources", ["scene_id"])
    op.create_index("ix_playback_sources_origin", "playback_sources", ["origin"])


def downgrade() -> None:
    op.drop_table("playback_sources")
