from __future__ import annotations

import json
from pathlib import Path

from refactor_master_helpers import ROOT, load_json


def main() -> int:
    dashboard = load_json(ROOT / "Temp" / "continuous_evaluation_dashboard_v1.json")
    pass100 = load_json(ROOT / "Temp" / "pass_100_criteria_v3.json")
    live_t20 = int(dashboard.get("live_t20_count") or 0)
    min_required = 30
    pass_100_allowed = bool(pass100.get("pass_100_allowed"))
    low_n_pass = live_t20 < min_required and pass_100_allowed
    result = {
        "formula_id": "LOW_N_PASS_GATE_V1",
        "live_t20_count": live_t20,
        "min_required": min_required,
        "pass_100_allowed": pass_100_allowed,
        "low_n_pass_count": 1 if low_n_pass else 0,
        "gate": "FAIL" if low_n_pass else "PASS",
    }
    out = ROOT / "Temp" / "low_n_pass_gate_v1.json"
    out.write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8")
    print(json.dumps(result, ensure_ascii=False, indent=2))
    return 0 if not low_n_pass else 1


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