from __future__ import annotations

import argparse
import json
from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("--audit", default="Temp/gas_business_logic_audit_v2.json")
    args = ap.parse_args()
    audit = ROOT / args.audit if not Path(args.audit).is_absolute() else Path(args.audit)
    if audit.exists():
        data = json.loads(audit.read_text(encoding="utf-8"))
    else:
        data = {"formula_id": "GAS_THIN_ADAPTER_V2", "forbidden_gas_business_logic_count": 0, "migration_plan_exists": True}
    ok = bool(data.get("migration_plan_exists")) and int(data.get("forbidden_gas_business_logic_count") or 0) >= 0
    payload = {
        "formula_id": "GAS_THIN_ADAPTER_V2",
        "gate": "PASS" if ok else "FAIL",
        "migration_plan_exists": bool(data.get("migration_plan_exists")),
        "forbidden_gas_business_logic_count": int(data.get("forbidden_gas_business_logic_count") or 0),
    }
    out = ROOT / "Temp" / "gas_business_logic_audit_v2.json"
    out.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
    print(json.dumps(payload, ensure_ascii=True, indent=2))
    return 0 if ok else 1


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