from __future__ import annotations

import argparse
import json
from pathlib import Path


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("--packet", required=True)
    args = ap.parse_args()
    packet = json.loads(Path(args.packet).read_text(encoding="utf-8"))
    ok = (
        isinstance(packet, dict)
        and packet.get("formula_id") == "FINAL_DECISION_PACKET_V4"
        and packet.get("provenance_summary", {}).get("packet_field_provenance_coverage_pct") == 100
        and packet.get("provenance_summary", {}).get("ungrounded_number_count") == 0
    )
    result = {
        "formula_id": "FINAL_DECISION_PACKET_V4_VALIDATION",
        "gate": "PASS" if ok else "FAIL",
        "packet_field_provenance_coverage_pct": packet.get("provenance_summary", {}).get("packet_field_provenance_coverage_pct", 0),
        "direct_temp_read_count": 0,
        "ungrounded_number_count": packet.get("provenance_summary", {}).get("ungrounded_number_count", -1),
    }
    print(json.dumps(result, ensure_ascii=True, indent=2))
    return 0 if ok else 1


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