"""download_jobs: link finished job to its imported track

Revision ID: 20260614_dl_track_id
Revises: 20260613_enrich_outcome
Create Date: 2026-06-14 10:00:00.000000

Adds ``download_jobs.track_id`` (nullable FK → ``tracks.id``) so a completed
download can point at the library track it produced — the §A5 download manager
links a "done" job to the track, and re-runs can tell a job already imported
(plan §6.1).
"""

from __future__ import annotations

from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op

revision: str = "20260614_dl_track_id"
down_revision: str | None = "20260613_enrich_outcome"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    op.add_column(
        "download_jobs",
        sa.Column("track_id", sa.Uuid(), nullable=True),
    )
    op.create_foreign_key(
        op.f("fk_download_jobs_track_id_tracks"),
        "download_jobs",
        "tracks",
        ["track_id"],
        ["id"],
        ondelete="SET NULL",
    )


def downgrade() -> None:
    op.drop_constraint(
        op.f("fk_download_jobs_track_id_tracks"),
        "download_jobs",
        type_="foreignkey",
    )
    op.drop_column("download_jobs", "track_id")
