"""per-user opt-in to full-history (deep) backfill

Revision ID: 0007_deep_requested
Revises: 0006_app_state
Create Date: 2026-06-11
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

revision: str = "0007_deep_requested"
down_revision: Union[str, None] = "0006_app_state"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.add_column(
        "subscriptions",
        sa.Column(
            "deep_requested",
            sa.Boolean(),
            nullable=False,
            server_default="false",
        ),
    )
    # Preserve today's behaviour: admins already had every channel deep-backfilling
    # globally, so opt their existing subscriptions in. Other users start recent-only.
    op.execute(
        "UPDATE subscriptions SET deep_requested = true "
        "WHERE user_id IN (SELECT id FROM users WHERE role = 'admin')"
    )


def downgrade() -> None:
    op.drop_column("subscriptions", "deep_requested")
