"""reset builtin download profiles: 720p is now the default (first) preset

Revision ID: 0058_dl_default_720p
Revises: 0057_plex_ppl_enriched
Create Date: 2026-07-22

The download dialog preselects the first builtin (lowest sort_order). Make 720p that default —
it plays everywhere, downloads faster, and is plenty for the shared /watch player — with 1080p and
Best still one click away. Re-seeds the builtin rows so fresh installs and existing DBs converge
(same mechanism as 0040). Existing jobs are unaffected (they snapshot their spec; a profile_id that
pointed at a replaced row just SET NULLs).
"""
from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

revision: str = "0058_dl_default_720p"
down_revision: Union[str, None] = "0057_plex_ppl_enriched"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def _spec(mode, height=None, container=None, audio_format=None):
    return {
        "mode": mode,
        "max_height": height,
        "container": container,
        "audio_format": audio_format,
        "vcodec": None,
        "embed_subs": False,
        "embed_chapters": False,
        "embed_thumbnail": False,
        "sponsorblock": False,
    }


# 720p first (the default preselection), then 1080p, Best, then the rest.
_BUILTINS = [
    ("720p (MP4)", _spec("av", 720, "mp4"), 10),
    ("1080p (MP4)", _spec("av", 1080, "mp4"), 20),
    ("Best (MP4)", _spec("av", None, "mp4"), 30),
    ("Audio (M4A)", _spec("a", None, None, "m4a"), 40),
    ("Audio (MP3)", _spec("a", None, None, "mp3"), 50),
    ("Video only (MP4)", _spec("v", None, "mp4"), 60),
]

_profiles = sa.table(
    "download_profiles",
    sa.column("user_id", sa.Integer),
    sa.column("name", sa.String),
    sa.column("spec", sa.JSON),
    sa.column("is_builtin", sa.Boolean),
    sa.column("sort_order", sa.Integer),
)


def upgrade() -> None:
    op.execute("DELETE FROM download_profiles WHERE is_builtin = true")
    op.bulk_insert(
        _profiles,
        [
            {"user_id": None, "name": n, "spec": s, "is_builtin": True, "sort_order": o}
            for n, s, o in _BUILTINS
        ],
    )


def downgrade() -> None:
    # No-op: the previous builtin set (1080p first) was seeded by 0040; not worth reconstructing.
    pass
