"""scene_play_progress — pozycja odtwarzania per scena (continue watching).

Revision ID: 0007_play_progress
Revises: 0006_blacklists
Create Date: 2026-05-04

Single-user; tabela trzyma ostatnio oglądane sceny + (gdy player zwróci) pozycję
w sekundach. Continue watching rail na home pobiera top-N ostatnich.
"""
from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op

revision: str = "0007_play_progress"
down_revision: str | None = "0006_blacklists"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    op.create_table(
        "scene_play_progress",
        sa.Column("scene_id", sa.dialects.postgresql.UUID(as_uuid=True),
                  sa.ForeignKey("scenes.id", ondelete="CASCADE"),
                  primary_key=True),
        sa.Column("position_sec", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("duration_sec", sa.Integer(), nullable=True),
        sa.Column("finished", sa.Boolean(), nullable=False, server_default=sa.false()),
        sa.Column("last_played_at", sa.DateTime(timezone=True),
                  server_default=sa.func.now(), nullable=False),
    )
    op.create_index(
        "ix_scene_play_progress_last_played_at",
        "scene_play_progress",
        ["last_played_at"],
    )


def downgrade() -> None:
    op.drop_index("ix_scene_play_progress_last_played_at", table_name="scene_play_progress")
    op.drop_table("scene_play_progress")
