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

import argparse
import json
from pathlib import Path


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


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("--json", default=str(DEFAULT_JSON))
    args = ap.parse_args()
    path = Path(args.json)
    data = json.loads(path.read_text(encoding="utf-8")) if path.exists() else {}
    ok = (
        float(data.get("module_io_schema_coverage_pct") or 0.0) >= 100.0
        and int(data.get("artifact_chain_count") or 0) >= 4
        and int(data.get("reverse_dependency_count") or 0) == 0
        and int(data.get("renderer_calculation_count") or 0) == 0
    )
    payload = {
        "formula_id": "ARCHITECTURE_BOUNDARIES_V2",
        "gate": "PASS" if ok else "FAIL",
        "renderer_calculation_count": int(data.get("renderer_calculation_count") or 0),
        "reverse_dependency_count": int(data.get("reverse_dependency_count") or 0),
        "module_io_schema_coverage_pct": float(data.get("module_io_schema_coverage_pct") or 0.0),
        "artifact_chain_count": int(data.get("artifact_chain_count") or 0),
    }
    print(json.dumps(payload, ensure_ascii=True, indent=2))
    return 0 if ok else 1


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