"""rename quota_events.action to the canonical <entity>_<action> taxonomy

Revision ID: 0020_rename_quota_actions
Revises: 0019_user_yt_channel_id
Create Date: 2026-06-19

The quota-attribution action keys were inconsistent (mixed verbs, ad-hoc names). Rename the
stored historical values to the canonical scheme defined in app.quota.QuotaAction:
``<entity>_<action>``, snake_case, explicit pull/push. Pure data migration on quota_events;
fully reversible. (Scheduler job ids and progress phase labels are a separate namespace and
are NOT touched.)
"""
from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

revision: str = "0020_rename_quota_actions"
down_revision: Union[str, None] = "0019_user_yt_channel_id"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None

# old action value -> new canonical key (mirrors app.quota.QuotaAction).
RENAMES: list[tuple[str, str]] = [
    ("sync_subscriptions", "subscriptions_pull"),
    ("subscription_resync", "subscriptions_resync"),
    ("playlist_sync", "playlists_pull"),
    ("playlist_push", "playlists_push"),
    ("playlist_delete", "playlists_delete"),
    ("backfill_recent", "videos_backfill_recent"),
    ("backfill_deep", "videos_backfill_full"),
    ("enrich", "videos_enrich"),
    ("video_lookup", "videos_lookup"),
    ("discovery", "channels_discover"),
    ("subscribe", "channels_subscribe"),
    ("unsubscribe", "channels_unsubscribe"),
    ("maintenance", "maintenance_revalidate"),
    ("api", "other"),
]

_SQL = sa.text("UPDATE quota_events SET action = :to WHERE action = :frm")


def upgrade() -> None:
    for frm, to in RENAMES:
        op.execute(_SQL.bindparams(frm=frm, to=to))


def downgrade() -> None:
    for frm, to in RENAMES:
        op.execute(_SQL.bindparams(frm=to, to=frm))
