"""Add ``id`` UUID PK to codal publisher tables.

These tables were created with ``publisher_id`` as the primary key, but the
versioning system (0028) inserts new rows per version and generates a UUID
``id`` for each.  This migration:

1. Drops the existing ``publisher_id`` primary key constraint
2. Adds an ``id`` UUID column
3. Populates existing rows with ``gen_random_uuid()``
4. Makes ``id`` NOT NULL
5. Sets ``id`` as the new primary key

Affected tables: ``codal_publishers``, ``codal_publisher_details``,
``codal_publisher_contacts``.

Revision ID: 0030
"""

from __future__ import annotations

from repo_core.migrations.builder import RawSqlOp
from repo_core.migrations.runner import Migration

VERSION = "0030"

# ---------------------------------------------------------------------------
# Shared SQL template — parameterized by table name
# ---------------------------------------------------------------------------


def _id_pk_migration_up(table: str) -> list[str]:
    return [
        f"ALTER TABLE {table} DROP CONSTRAINT IF EXISTS {table}_pkey",
        f"ALTER TABLE {table} ADD COLUMN IF NOT EXISTS id UUID",
        f"UPDATE {table} SET id = gen_random_uuid() WHERE id IS NULL",
        f"ALTER TABLE {table} ALTER COLUMN id SET NOT NULL",
        f"ALTER TABLE {table} ADD PRIMARY KEY (id)",
    ]


def get_postgres_migrations() -> list[Migration]:
    return [
        Migration(
            version="codal_publishers_0006",
            description=(
                "Add id UUID column and change PK from publisher_id to id "
                "on codal_publishers"
            ),
            up=RawSqlOp(_id_pk_migration_up("codal_publishers")),
            # Irreversible — once multiple versions exist, publisher_id
            # is no longer unique.
            down=None,
        ),
        Migration(
            version="codal_publisher_details_0004",
            description=(
                "Add id UUID column and change PK from publisher_id to id "
                "on codal_publisher_details"
            ),
            up=RawSqlOp(_id_pk_migration_up("codal_publisher_details")),
            down=None,
        ),
        Migration(
            version="codal_publisher_contacts_0003",
            description=(
                "Add id UUID column and change PK from publisher_id to id "
                "on codal_publisher_contacts"
            ),
            up=RawSqlOp(_id_pk_migration_up("codal_publisher_contacts")),
            down=None,
        ),
        # ── codal_publisher_similar: fix NOT NULL without DEFAULT ───
        Migration(
            version="codal_publisher_similar_0003",
            description=(
                "Set DEFAULT 0 on codal_publisher_similar.similar_publisher_id "
                "so sentinel rows (empty results) can be inserted"
            ),
            up=RawSqlOp(
                "ALTER TABLE codal_publisher_similar "
                "ALTER COLUMN similar_publisher_id SET DEFAULT 0"
            ),
            down=RawSqlOp(
                "ALTER TABLE codal_publisher_similar "
                "ALTER COLUMN similar_publisher_id DROP DEFAULT"
            ),
        ),
    ]
