"""Generate tests/testthat/golden_specs.R: the per-indicator spec list (canonical
name, archetype, ctor params, output width) consumed by test-golden-all.R, which
replays all 514 indicators against the Rust reference fixtures g_<Canonical>.csv.

Run from repo root:  python bindings/r/gen_golden_test.py
"""
import json
import os

ROOT = os.path.normpath(os.path.join(os.path.dirname(__file__), "..", ".."))
MAN = json.load(open(os.path.join(ROOT, "testdata", "golden", "golden_manifest.json")))
# Canonical core Indicator::name() per indicator, shared across every binding.
NAMES = json.load(open(os.path.join(ROOT, "testdata", "golden", "names.json")))

lines = [
    "# Code generated by gen_golden_test.py. DO NOT EDIT.",
    "# Per-indicator golden spec: canonical name, archetype, ctor params, output width, core name.",
    "GOLDEN_SPECS <- list(",
]
for e in MAN:
    n = e.get("n", e.get("width", 0))
    params = ",".join(repr(float(p)) for p in e["params"])
    nm = NAMES[e["canonical"]].replace("\\", "\\\\").replace('"', '\\"')
    lines.append(
        f'  list(canon="{e["canonical"]}", arch="{e["arch"]}", params=c({params}), width={n}L, name="{nm}"),'
    )
lines[-1] = lines[-1].rstrip(",")
lines.append(")")

dest = os.path.join(ROOT, "bindings", "r", "tests", "testthat", "golden_specs.R")
open(dest, "w", encoding="utf-8").write("\n".join(lines) + "\n")
print("wrote golden_specs.R with", len(MAN), "specs")
