#!/usr/bin/env python3
"""reproduce_receipt_verify.py — feeless402 PR#4 receipt.verify() bug.

Demonstrates that nano_pay.receipt.verify() raises Mismatch on a REAL
confirmed send, because it compares the caller's `account` (the payee the
seller expected) against block_account — which in block_info is the block's
AUTHOR (the payer), not the destination.

The canned reply below is copied verbatim from a live block_info answer
(rpc.nano.to / rainstorm.city, 2026-09-26) for pursekeeper's confirmed
2.499999-XNO send F68B9E8B...0BA7EDA (its parley member-pass payment).

Run from a feeless402 checkout:
    python3 reproduce_receipt_verify.py            # against master  -> Mismatch
    # apply receipt_verify_fix.patch, then again   # against fixed   -> settled
"""
import os, sys

sys.path.insert(0, os.getcwd())  # run from a feeless402 checkout root
try:
    from nano_pay import receipt
except ImportError as e:  # nano_pay.rpc needs `requests`; stub it if absent
    import types
    sys.modules["requests"] = types.ModuleType("requests")
    sys.modules["requests"].post = lambda *a, **k: (_ for _ in ()).throw(
        RuntimeError("offline reproducer"))
    from nano_pay import receipt

HASH = "F68B9E8B7E1DB80747E611DF7712E79AB14F5AFFF313DF6E4681904704BA7EDA"
PAYER = "nano_1xug1q5t7nxoj3ywwzokiea9jz8fq8qfgzp8pbyfr3co3e5xgj755uofu8ue"
PAYEE = "nano_1w3xqs35iapy11kwurkgnt9gr9auzqf57pwi7w54u3tzb88piqe3h1b9gqfg"
AMOUNT = 2499999323300000000000000000000

LIVE_BLOCK_INFO = {
    "block_account": PAYER,
    "amount": str(AMOUNT),
    "balance": "110146180344318570000000000000001",
    "height": "251",
    "local_timestamp": "1790462254",
    "confirmed": "true",
    "subtype": "send",
    "contents": {
        "type": "state",
        "account": PAYER,
        "link_as_account": PAYEE,
        "link": "703DBE423822DE0025CDE24EA68EEC1D1BFDDA32DB902F062D875F498D685D81",
    },
}

receipt._lookup = lambda *a, **k: LIVE_BLOCK_INFO

try:
    r = receipt.verify(HASH, AMOUNT, PAYEE, "https://example.invalid/rpc")
except receipt.Mismatch as e:
    print(f"BUG REPRODUCED: verify() -> Mismatch({e})")
    print("  got =", e.got, "(block_account = the PAYER, i.e. the block author)")
    print("  expected =", e.expected, "(the documented payee)")
    sys.exit(1)

print(f"FIXED: verify() -> settled={r.settled} amount={r.amount_raw} account={r.account}")
sys.exit(0)
