"""per-user search finds

Revision ID: 0030_search_finds
Revises: 0029_unaccent_search
Create Date: 2026-06-30

Adds the `search_finds` table: a per-user record of which videos a user surfaced via their own
live YouTube search. This lets "videos I searched for" appear in that user's Mine feed (and the
Mine Source filter), independent of the global `videos.via_search` flag (which marks a video as
search-discovered by anyone, for the shared Library's Source filter).
"""
from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

revision: str = "0030_search_finds"
down_revision: Union[str, None] = "0029_unaccent_search"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        "search_finds",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("video_id", sa.String(), nullable=False),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["video_id"], ["videos.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("user_id", "video_id", name="uq_user_search_video"),
    )
    op.create_index("ix_search_finds_user_id", "search_finds", ["user_id"])
    op.create_index("ix_search_finds_video_id", "search_finds", ["video_id"])


def downgrade() -> None:
    op.drop_index("ix_search_finds_video_id", table_name="search_finds")
    op.drop_index("ix_search_finds_user_id", table_name="search_finds")
    op.drop_table("search_finds")
