"""saved searches: per-device saved keyword queries

Revision ID: 0024_saved_searches
Revises: 0023_bug_report_replies
Create Date: 2026-06-16

Zapisane słowa kluczowe per urządzenie (user-report mobilism: sceny słabo opisane →
dodatkowe strategie wyszukiwania). Scope po device_id (X-Device-Id), unikat na
(device_id, query) żeby ten sam zapis był idempotentny.
"""
from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

revision: str = "0024_saved_searches"
down_revision: str | None = "0023_bug_report_replies"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    op.create_table(
        "saved_searches",
        sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("device_id", sa.String(length=64), nullable=False),
        sa.Column("query", sa.String(length=256), nullable=False),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            server_default=sa.func.now(),
            nullable=False,
        ),
        sa.PrimaryKeyConstraint("id", name="pk_saved_searches"),
        sa.UniqueConstraint("device_id", "query", name="uq_saved_searches_device_query"),
    )
    op.create_index("ix_saved_searches_device_id", "saved_searches", ["device_id"])


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