"""Shared CLI and SQL helpers for migration modes."""

from __future__ import annotations

import argparse


def parse_mode() -> tuple[str, bool]:
    parser = argparse.ArgumentParser()
    parser.add_argument("--env", default="dev")
    parser.add_argument("--sync", action="store_true")
    args = parser.parse_args()
    return args.env, args.sync


def existing_ids(cursor, table: str, key_columns: tuple[str, ...], where: str = "") -> dict[tuple, str]:
    columns = ",".join(("id", *key_columns))
    cursor.execute(f"SELECT {columns} FROM {table} {where}")
    return {tuple(row[1:]): row[0] for row in cursor.fetchall()}


def delete_missing(cursor, table: str, key_columns: tuple[str, ...], seen: set[tuple], where: str) -> int:
    rows = existing_ids(cursor, table, key_columns, where)
    stale = [row_id for key, row_id in rows.items() if key not in seen]
    if stale:
        cursor.executemany(f"DELETE FROM {table} WHERE id=%s", [(row_id,) for row_id in stale])
    return len(stale)
