"""watch progress: resume position on video_states

Revision ID: 0010_watch_progress
Revises: 0009_quota_events
Create Date: 2026-06-14
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

revision: str = "0010_watch_progress"
down_revision: Union[str, None] = "0009_quota_events"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.add_column(
        "video_states",
        sa.Column(
            "position_seconds",
            sa.Integer(),
            nullable=False,
            server_default="0",
        ),
    )
    op.add_column(
        "video_states",
        sa.Column("progress_updated_at", sa.DateTime(timezone=True), nullable=True),
    )
    # Partial index to keep the "in progress" feed filter cheap (only started videos).
    op.create_index(
        "ix_video_states_in_progress",
        "video_states",
        ["user_id"],
        postgresql_where=sa.text("position_seconds > 0"),
    )


def downgrade() -> None:
    op.drop_index("ix_video_states_in_progress", table_name="video_states")
    op.drop_column("video_states", "progress_updated_at")
    op.drop_column("video_states", "position_seconds")
