#!/usr/bin/env python3
from __future__ import annotations

import argparse
import json
import subprocess
import sys
from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]
DEFAULT_REPORT = ROOT / "Temp" / "operational_report.json"


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("--report", default=str(DEFAULT_REPORT))
    args = ap.parse_args()

    cmd = [sys.executable, str(ROOT / "tools" / "validate_renderer_section_order_v1.py"), "--report", args.report]
    proc = subprocess.run(cmd, cwd=ROOT, capture_output=True, text=True, encoding="utf-8", errors="replace")
    payload = {
        "formula_id": "LOW_CAPABILITY_RESPONSE_CONTRACT_V3",
        "gate": "PASS" if proc.returncode == 0 else "FAIL",
        "delegate": "validate_renderer_section_order_v1.py",
        "delegate_returncode": proc.returncode,
        "delegate_stdout": proc.stdout.strip(),
        "delegate_stderr": proc.stderr.strip(),
    }
    print(json.dumps(payload, ensure_ascii=False, indent=2))
    return proc.returncode


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