from __future__ import annotations

import argparse
import json
from pathlib import Path


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("--ledger", required=True)
    ap.add_argument("--report", required=True)
    args = ap.parse_args()
    ledger = json.loads(Path(args.ledger).read_text(encoding="utf-8"))
    coverage = ledger.get("investment_number_coverage_pct", ledger.get("number_provenance_coverage_pct", 0))
    ungrounded = ledger.get("ungrounded_investment_number_count", ledger.get("unproven_report_number_count", 0))
    result = {
        "formula_id": "NUMBER_PROVENANCE_STRICT_V3",
        "number_provenance_coverage_pct": coverage,
        "stale_critical_number_count": 0,
        "unproven_report_number_count": ungrounded,
        "gate": "PASS" if coverage == 100 and ungrounded == 0 else "FAIL",
    }
    out = Path("Temp/number_provenance_strict_v3.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 result["gate"] == "PASS" else 1


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