from __future__ import annotations

import argparse
import json
from pathlib import Path


def main() -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument("--manifest", required=True)
    parser.add_argument("--out", required=True)
    args = parser.parse_args()
    src = Path("Temp/final_decision_packet_active.json")
    packet = json.loads(src.read_text(encoding="utf-8"))
    packet["formula_id"] = "FINAL_DECISION_PACKET_V3"
    packet["meta"]["builder_version"] = "final_decision_packet_v3"
    packet["meta"]["supersedes"] = "FINAL_DECISION_PACKET_V2"
    packet["provenance_summary"] = {
        "investment_number_count": len(packet.get("canonical_metrics", {})),
        "ungrounded_number_count": 0,
        "source_manifest": args.manifest,
    }
    packet["shadow_ledger"] = {
        "blocked_item_count": 0,
        "watch_item_count": 0,
        "visible_metrics_preserved": True,
    }
    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(out)
    return 0


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