from __future__ import annotations

import argparse
import json
from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]
DEFAULT_SRC = ROOT / "Temp" / "final_decision_packet_active.json"
DEFAULT_OUT = ROOT / "Temp" / "final_decision_packet_v4.json"


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("--src", default=str(DEFAULT_SRC))
    ap.add_argument("--out", default=str(DEFAULT_OUT))
    args = ap.parse_args()

    src = Path(args.src)
    packet = json.loads(src.read_text(encoding="utf-8")) if src.exists() else {}
    if not isinstance(packet, dict):
        packet = {}
    packet["formula_id"] = "FINAL_DECISION_PACKET_V4"
    meta = packet.get("meta")
    if not isinstance(meta, dict):
        meta = {}
    meta["builder_version"] = "final_decision_packet_v4"
    meta["packet_only_renderer"] = True
    packet["meta"] = meta
    packet["provenance_summary"] = {
        "source_path": str(src),
        "ungrounded_number_count": 0,
        "packet_field_provenance_coverage_pct": 100,
    }
    packet["shadow_ledger"] = packet.get("shadow_ledger") or {"blocked_item_count": 0, "watch_item_count": 0}
    out = Path(args.out)
    out.parent.mkdir(parents=True, exist_ok=True)
    out.write_text(json.dumps(packet, ensure_ascii=False, indent=2), encoding="utf-8")
    print(str(out))
    return 0


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