#!/usr/bin/env python3
"""Generate PUBLIC_API.json from the live ``__all__`` of mgf.secrets (DOC-08).

Records the public surface so the federation's drift + surface-tracking checks have
a baseline. Regenerate after any change to ``mgf.secrets.__all__``.
"""

from __future__ import annotations

import importlib
import json
from pathlib import Path
from typing import Any


def build_payload() -> dict[str, Any]:
    mod = importlib.import_module("mgf.secrets")
    names = sorted(mod.__all__)
    return {
        "__version__": mod.__version__,
        "modules": {"mgf.secrets": names},
        "__generated_total_names": len(names),
    }


def write_json(path: Path) -> None:
    path.write_text(json.dumps(build_payload(), indent=2, sort_keys=True) + "\n", encoding="utf-8")


if __name__ == "__main__":
    target = Path(__file__).resolve().parent.parent / "PUBLIC_API.json"
    write_json(target)
    print(f"wrote {target}")
