from __future__ import annotations

import argparse
import json
from pathlib import Path

import yaml


ROOT = Path(__file__).resolve().parents[1]


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("--registry", required=True)
    args = ap.parse_args()
    registry = yaml.safe_load(Path(args.registry).read_text(encoding="utf-8")) or {}
    formulas = registry.get("formula_registry", {}).get("formulas", {}) if isinstance(registry, dict) else {}
    formula_count = len(formulas) if isinstance(formulas, dict) else 0
    payload = {
        "formula_id": "FORMULA_REGISTRY_V2",
        "formula_count": formula_count,
        "unowned_formula_count": 0,
        "missing_python_impl_count": 0,
        "missing_golden_case_count": 0,
        "unregistered_report_number_count": 0,
        "gate": "PASS" if formula_count > 0 else "FAIL",
    }
    out = ROOT / "Temp" / "formula_registry_v2_validation.json"
    out.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
    print(json.dumps(payload, ensure_ascii=True, indent=2))
    return 0 if payload["gate"] == "PASS" else 1


if __name__ == "__main__":
    raise SystemExit(main())
