"""ingest core: channels, subscriptions, videos, api_quota_usage

Revision ID: 0002_ingest_core
Revises: 0001_initial
Create Date: 2026-06-11
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

revision: str = "0002_ingest_core"
down_revision: Union[str, None] = "0001_initial"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        "channels",
        sa.Column("id", sa.String(length=32), primary_key=True),
        sa.Column("title", sa.String(length=255), nullable=True),
        sa.Column("handle", sa.String(length=255), nullable=True),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("thumbnail_url", sa.String(length=1024), nullable=True),
        sa.Column("uploads_playlist_id", sa.String(length=64), nullable=True),
        sa.Column("subscriber_count", sa.BigInteger(), nullable=True),
        sa.Column("video_count", sa.BigInteger(), nullable=True),
        sa.Column("topic_categories", sa.JSON(), nullable=True),
        sa.Column("default_language", sa.String(length=16), nullable=True),
        sa.Column("country", sa.String(length=8), nullable=True),
        sa.Column("details_synced_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("recent_synced_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("last_rss_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("backfill_cursor", sa.String(length=128), nullable=True),
        sa.Column("backfill_done", sa.Boolean(), nullable=False, server_default="false"),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            server_default=sa.func.now(),
            nullable=False,
        ),
    )

    op.create_table(
        "subscriptions",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column(
            "user_id",
            sa.Integer(),
            sa.ForeignKey("users.id", ondelete="CASCADE"),
            nullable=False,
        ),
        sa.Column(
            "channel_id",
            sa.String(length=32),
            sa.ForeignKey("channels.id", ondelete="CASCADE"),
            nullable=False,
        ),
        sa.Column("yt_subscription_id", sa.String(length=128), nullable=True),
        sa.Column("priority", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("hidden", sa.Boolean(), nullable=False, server_default="false"),
        sa.Column("subscribed_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            server_default=sa.func.now(),
            nullable=False,
        ),
        sa.UniqueConstraint("user_id", "channel_id", name="uq_user_channel"),
    )
    op.create_index("ix_subscriptions_user_id", "subscriptions", ["user_id"])
    op.create_index("ix_subscriptions_channel_id", "subscriptions", ["channel_id"])

    op.create_table(
        "videos",
        sa.Column("id", sa.String(length=16), primary_key=True),
        sa.Column(
            "channel_id",
            sa.String(length=32),
            sa.ForeignKey("channels.id", ondelete="CASCADE"),
            nullable=False,
        ),
        sa.Column("title", sa.Text(), nullable=True),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("published_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("thumbnail_url", sa.String(length=1024), nullable=True),
        sa.Column("duration_seconds", sa.Integer(), nullable=True),
        sa.Column("view_count", sa.BigInteger(), nullable=True),
        sa.Column("like_count", sa.BigInteger(), nullable=True),
        sa.Column("category_id", sa.Integer(), nullable=True),
        sa.Column("topic_categories", sa.JSON(), nullable=True),
        sa.Column("default_language", sa.String(length=16), nullable=True),
        sa.Column("detected_language", sa.String(length=16), nullable=True),
        sa.Column("is_short", sa.Boolean(), nullable=False, server_default="false"),
        sa.Column("live_status", sa.String(length=16), nullable=False, server_default="none"),
        sa.Column("enriched_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            server_default=sa.func.now(),
            nullable=False,
        ),
    )
    op.create_index("ix_videos_channel_id", "videos", ["channel_id"])
    op.create_index("ix_videos_published_at", "videos", ["published_at"])
    op.create_index("ix_videos_is_short", "videos", ["is_short"])
    op.create_index("ix_videos_live_status", "videos", ["live_status"])

    op.create_table(
        "api_quota_usage",
        sa.Column("day", sa.Date(), primary_key=True),
        sa.Column("units_used", sa.Integer(), nullable=False, server_default="0"),
    )


def downgrade() -> None:
    op.drop_table("api_quota_usage")
    op.drop_index("ix_videos_live_status", table_name="videos")
    op.drop_index("ix_videos_is_short", table_name="videos")
    op.drop_index("ix_videos_published_at", table_name="videos")
    op.drop_index("ix_videos_channel_id", table_name="videos")
    op.drop_table("videos")
    op.drop_index("ix_subscriptions_channel_id", table_name="subscriptions")
    op.drop_index("ix_subscriptions_user_id", table_name="subscriptions")
    op.drop_table("subscriptions")
    op.drop_table("channels")
