from __future__ import annotations

import argparse
import json
from pathlib import Path

from build_goal_risk_budget_harness_v2 import main as build_v2_main


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


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("--json", default="GatherTradingData.json")
    ap.add_argument("--truth", default="Temp/operational_truth_score_v1.json")
    ap.add_argument("--out", default="Temp/goal_risk_budget_harness_v3.json")
    args = ap.parse_args()
    # Reuse v2 builder for the current deterministic payload, then alias to v3 output.
    build_v2_main()
    src = ROOT / "Temp" / "goal_risk_budget_harness_v2.json"
    payload = json.loads(src.read_text(encoding="utf-8")) if src.exists() else {}
    if not isinstance(payload, dict):
        payload = {}
    payload["formula_id"] = "GOAL_RISK_BUDGET_HARNESS_V3"
    out = ROOT / args.out
    out.parent.mkdir(parents=True, exist_ok=True)
    out.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
    print(json.dumps({"formula_id": payload.get("formula_id"), "out": str(out)}, ensure_ascii=True))
    return 0


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