#!/usr/bin/env python3
"""Verify the packet's exact unsigned Bulla ActionReceipt to digest depth."""

from __future__ import annotations

import json
from pathlib import Path
import sys


def main() -> int:
    if len(sys.argv) != 3:
        raise SystemExit("usage: verify-promise.py PACKET_ROOT RECEIPT")
    root = Path(sys.argv[1]).resolve()
    sys.path.insert(0, str(root / "verifiers" / "bulla-source"))
    from bulla.action_receipt import verify_receipt  # noqa: PLC0415

    receipt = json.loads(Path(sys.argv[2]).read_text(encoding="utf-8"))
    result = verify_receipt(receipt)
    if not result.ok or result.verified_to != "digest":
        raise SystemExit("promise ActionReceipt did not verify to exact digest depth")
    print(json.dumps({
        "ok": result.ok,
        "verified_to": result.verified_to,
        "attestation": receipt["hashes"]["attestation"],
        "transaction_id": receipt["action"]["subject"]["transaction_id"],
        "amount": receipt["action"]["subject"]["amount"],
        "model_commitment": receipt["action"]["subject"]["model_commitment"],
        "request_commitment": receipt["action"]["subject"]["request_commitment"],
        "unit": receipt["action"]["subject"]["unit"],
    }, sort_keys=True))
    return 0


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