from __future__ import annotations

import json
from pathlib import Path

from refactor_master_helpers import ROOT, load_json


def main() -> int:
    gate = load_json(ROOT / "Temp" / "engine_harness_gate_result.json")
    failed_checks = gate.get("failed_checks", []) if isinstance(gate.get("failed_checks"), list) else []
    triage = []
    for item in failed_checks:
        if not isinstance(item, dict):
            continue
        triage.append({
            "check": item.get("name") or item.get("check") or "UNKNOWN",
            "category": "DATA_GATED",
            "owner": "ops",
            "next_todo": "Collect required live samples or resolve source gate",
        })
    result = {
        "formula_id": "FAILURE_TRIAGE_V1",
        "triage_count": len(triage),
        "triage": triage,
        "gate": "PASS",
    }
    out = ROOT / "Temp" / "failure_triage_v1.json"
    out.write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8")
    print(json.dumps(result, ensure_ascii=True, indent=2))
    return 0


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