"""device-scope user state: favorites / play-progress / blacklists

Revision ID: 0022_device_scoped_user_state
Revises: 0021_scene_tags_tag_id_stats
Create Date: 2026-06-08

Publiczna instancja nie ma kont → stan usera był GLOBALNY, nowi użytkownicy
nadpisywali ulubione/blacklisty/progress Jana (bug 2026-06-08). Dodajemy `device_id`
(VARCHAR 64) do 9 tabel stanu i przerabiamy PK na composite `(device_id, <entity>)`.

Istniejące wiersze → `device_id = 'legacy-shared'` (sentinel). Apka po update wysyła
`X-Device-Id`; `/me/adopt-legacy` przepina legacy na docelowe device. Zero utraty
danych (backfill + composite PK, nie drop).
"""
from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op

revision: str = "0022_device_scoped_user_state"
down_revision: str | None = "0021_scene_tags_tag_id_stats"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None

LEGACY = "legacy-shared"

# (table, entity_pk_column)
_TABLES: list[tuple[str, str]] = [
    ("favorite_performers", "performer_id"),
    ("favorite_studios", "studio_id"),
    ("favorite_scenes", "scene_id"),
    ("favorite_movies", "movie_id"),
    ("scene_play_progress", "scene_id"),
    ("movie_play_progress", "movie_id"),
    ("blacklisted_performers", "performer_id"),
    ("blacklisted_studios", "studio_id"),
    ("blacklisted_tags", "tag_id"),
]


def upgrade() -> None:
    for table, entity in _TABLES:
        # 1. dodaj nullable, 2. backfill legacy, 3. NOT NULL, 4. composite PK
        op.add_column(table, sa.Column("device_id", sa.String(length=64), nullable=True))
        op.execute(sa.text(f"UPDATE {table} SET device_id = :d").bindparams(d=LEGACY))
        op.alter_column(table, "device_id", nullable=False)
        op.drop_constraint(f"pk_{table}", table, type_="primary")
        op.create_primary_key(f"pk_{table}", table, ["device_id", entity])


def downgrade() -> None:
    for table, entity in _TABLES:
        op.drop_constraint(f"pk_{table}", table, type_="primary")
        # przywróć single-col PK (zakłada brak duplikatów entity po dropie device_id)
        op.execute(sa.text(f"DELETE FROM {table} a USING {table} b "
                           f"WHERE a.ctid < b.ctid AND a.{entity} = b.{entity}"))
        op.create_primary_key(f"pk_{table}", table, [entity])
        op.drop_column(table, "device_id")
