"""channel explore: about-page fields, explore provenance, explored_channels

Revision ID: 0033_channel_explore
Revises: 0032_search_document
Create Date: 2026-06-30

Supports the channel-explore feature (a dedicated channel page + ephemeral browsing of
un-subscribed channels):
  - channels.total_view_count / published_at / banner_url / external_links — richer "About"
    metadata fetched from channels.list (part=statistics, snippet, brandingSettings).
  - channels.from_explore — this channel exists only because someone explored it (not an
    organic subscription / not a search find); drives the ephemeral cleanup job.
  - videos.via_explore — this video entered the catalog via a channel exploration. Hidden
    from everyone's Library by default; visible only to users who explored (or subscribe to)
    the channel (a per-user gate in the feed query).
  - explored_channels — per-user record of "I explored this channel": gates channel-page
    access to its ephemeral videos and drives the grace-period purge of un-kept channels.
All new flags default false / NULL, so every existing row stays organic.
"""
from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

revision: str = "0033_channel_explore"
down_revision: Union[str, None] = "0032_search_document"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.add_column("channels", sa.Column("total_view_count", sa.BigInteger(), nullable=True))
    op.add_column(
        "channels", sa.Column("published_at", sa.DateTime(timezone=True), nullable=True)
    )
    op.add_column("channels", sa.Column("banner_url", sa.String(length=1024), nullable=True))
    op.add_column("channels", sa.Column("external_links", sa.JSON(), nullable=True))
    op.add_column(
        "channels",
        sa.Column("from_explore", sa.Boolean(), nullable=False, server_default="false"),
    )
    op.create_index("ix_channels_from_explore", "channels", ["from_explore"])

    op.add_column(
        "videos",
        sa.Column("via_explore", sa.Boolean(), nullable=False, server_default="false"),
    )
    op.create_index("ix_videos_via_explore", "videos", ["via_explore"])

    op.create_table(
        "explored_channels",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("channel_id", sa.String(length=32), 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(["channel_id"], ["channels.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("user_id", "channel_id", name="uq_user_explored_channel"),
    )
    op.create_index("ix_explored_channels_user_id", "explored_channels", ["user_id"])
    op.create_index("ix_explored_channels_channel_id", "explored_channels", ["channel_id"])


def downgrade() -> None:
    op.drop_index("ix_explored_channels_channel_id", table_name="explored_channels")
    op.drop_index("ix_explored_channels_user_id", table_name="explored_channels")
    op.drop_table("explored_channels")

    op.drop_index("ix_videos_via_explore", table_name="videos")
    op.drop_column("videos", "via_explore")

    op.drop_index("ix_channels_from_explore", table_name="channels")
    op.drop_column("channels", "from_explore")
    op.drop_column("channels", "external_links")
    op.drop_column("channels", "banner_url")
    op.drop_column("channels", "published_at")
    op.drop_column("channels", "total_view_count")
