"""user preferences + per-user video watch state

Revision ID: 0004_feed_state_prefs
Revises: 0003_tags
Create Date: 2026-06-11
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

revision: str = "0004_feed_state_prefs"
down_revision: Union[str, None] = "0003_tags"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.add_column("users", sa.Column("preferences", sa.JSON(), nullable=True))

    op.create_table(
        "video_states",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column(
            "user_id",
            sa.Integer(),
            sa.ForeignKey("users.id", ondelete="CASCADE"),
            nullable=False,
        ),
        sa.Column(
            "video_id",
            sa.String(length=16),
            sa.ForeignKey("videos.id", ondelete="CASCADE"),
            nullable=False,
        ),
        sa.Column("status", sa.String(length=12), nullable=False, server_default="new"),
        sa.Column("watched_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column(
            "updated_at",
            sa.DateTime(timezone=True),
            server_default=sa.func.now(),
            nullable=False,
        ),
        sa.UniqueConstraint("user_id", "video_id", name="uq_user_video"),
    )
    op.create_index("ix_video_states_user_id", "video_states", ["user_id"])
    op.create_index("ix_video_states_video_id", "video_states", ["video_id"])


def downgrade() -> None:
    op.drop_table("video_states")
    op.drop_column("users", "preferences")
