"""Plex collections

Revision ID: 0047_plex_collections
Revises: 0046_plex_people_search
Create Date: 2026-07-06

Mirrors Plex collections locally so reads never hit Plex's slow (dual-language) collection queries.
A `plex_collections` table holds each collection's card metadata; membership is stored as GIN-indexed
`collection_keys` on the member movies (plex_items) and shows (plex_shows), so a collection can be a
combinable filter and a title's "collection strip" is a pure local lookup. `editable` marks the
(curated) collections we may later write back to Plex; everything is read-only by default.
"""
from typing import Sequence, Union

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

revision: str = "0047_plex_collections"
down_revision: Union[str, None] = "0046_plex_people_search"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        "plex_collections",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("rating_key", sa.String(length=32), nullable=False),
        sa.Column("library_id", sa.Integer(), sa.ForeignKey("plex_libraries.id", ondelete="CASCADE"), nullable=False),
        sa.Column("title", sa.String(length=512), nullable=False),
        sa.Column("summary", sa.Text(), nullable=True),
        sa.Column("thumb_key", sa.String(length=512), nullable=True),
        sa.Column("child_count", sa.Integer(), nullable=True),
        sa.Column("smart", sa.Boolean(), nullable=False, server_default="false"),
        sa.Column("editable", sa.Boolean(), nullable=False, server_default="false"),
        sa.Column("synced_at", sa.DateTime(timezone=True), nullable=True),
        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),
    )
    op.create_index("ix_plex_collections_rating_key", "plex_collections", ["rating_key"], unique=True)
    op.create_index("ix_plex_collections_library_id", "plex_collections", ["library_id"])

    op.add_column("plex_items", sa.Column("collection_keys", postgresql.JSONB(), nullable=True))
    op.create_index("ix_plex_items_collections_gin", "plex_items", ["collection_keys"], postgresql_using="gin")
    op.add_column("plex_shows", sa.Column("collection_keys", postgresql.JSONB(), nullable=True))
    op.create_index("ix_plex_shows_collections_gin", "plex_shows", ["collection_keys"], postgresql_using="gin")


def downgrade() -> None:
    op.drop_index("ix_plex_shows_collections_gin", table_name="plex_shows")
    op.drop_column("plex_shows", "collection_keys")
    op.drop_index("ix_plex_items_collections_gin", table_name="plex_items")
    op.drop_column("plex_items", "collection_keys")
    op.drop_index("ix_plex_collections_library_id", table_name="plex_collections")
    op.drop_index("ix_plex_collections_rating_key", table_name="plex_collections")
    op.drop_table("plex_collections")
