"""realdebrid_cache — direct stream URL cache dla RD /unrestrict/link wyników

Revision ID: 0016_realdebrid_cache
Revises: 0015_bug_reports_movie_id
Create Date: 2026-05-12

RD direct linki technically valid ~7 dni, ale cache'ujemy 24h (configurable
przez RD_CACHE_TTL_HOURS) żeby oszczędzać API quota przy replay tej samej
sceny.
"""
from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op

revision: str = "0016_realdebrid_cache"
down_revision: str | None = "0015_bug_reports_movie_id"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    op.create_table(
        "realdebrid_cache",
        sa.Column("hoster_url", sa.Text(), primary_key=True),
        sa.Column("direct_url", sa.Text(), nullable=False),
        sa.Column(
            "created_at",
            sa.TIMESTAMP(timezone=True),
            nullable=False,
            server_default=sa.text("now()"),
        ),
        sa.Column("expires_at", sa.TIMESTAMP(timezone=True), nullable=False),
    )
    op.create_index(
        "ix_realdebrid_cache_expires_at",
        "realdebrid_cache",
        ["expires_at"],
    )


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