#!/usr/bin/env python3
"""reproduce_aibtc_relay_wedge.py — reproduce report: aibtc x402 facilitator
relay wedge of 2026-09-28. Read-only GETs, stdlib only, no auth.

Prints three checks from the report:
  1. Relay status of the seeded staged payment (still held? global nonce gap?)
  2. Live submissionCount on the 9 paid-query-dependent bounties
  3. ARION sender mempool depth + last confirmed nonce (Hiro)
"""
import json, urllib.request

UA = {"User-Agent": "ARION-report-reproducer/1.0"}
RELAY = "https://x402-relay.aibtc.com/payment/pay_3dc604f860a64b3aade831462fd03c8f"
FEED = "https://aibtc.com/api/bounties?limit=100"
SENDER = "SP3SAQ4K1YQJG2451WPM72T6M4V0A6JJWTGDSY2E5"
QUERY_SLOTS = [
    "mukrrrze1aaffcc1ff00", "mukrs2f421387fe72d76", "mukrsai3842023af8b82",
    "mukrsipfdf7a235a814e", "mukrsqxu9e8befa8b78a", "mukrsz4zcdada639bdc7",
    "mukrt7v2a1220ffc0c3d", "mukrtg19a43bb5456ecf", "mukrreo32660047de323",
]


def get(url):
    req = urllib.request.Request(url, headers=UA)
    with urllib.request.urlopen(req, timeout=25) as r:
        return json.load(r)


def main():
    print("== 1. relay status (seeded staged payment) ==")
    try:
        s = get(RELAY)
        for k in ("status", "relayState", "holdReason", "error",
                  "holdExpiresAt", "nextExpectedNonce", "missingNonces"):
            print(f"  {k}: {s.get(k)}")
        print(f"  senderNonceInfo: {s.get('senderNonceInfo')}")
    except Exception as e:
        print(f"  relay status fetch failed: {e} (payment may have been swept — wedge over)")

    print("\n== 2. paid-query race slots (public bounty feed) ==")
    rows = get(FEED)
    rows = rows if isinstance(rows, list) else rows.get("bounties", [])
    byid = {b.get("id"): b for b in rows}
    for bid in QUERY_SLOTS:
        b = byid.get(bid)
        if b:
            print(f"  {bid} status={b.get('status')} subs={b.get('submissionCount')} | {b.get('title', '')[:60]}")
        else:
            print(f"  {bid} not in feed (closed/expired?)")

    print("\n== 3. sender mempool + last confirmed nonce ==")
    try:
        mp = get(f"https://api.hiro.so/extended/v1/address/{SENDER}/mempool")
        print(f"  mempool txs: {len(mp.get('results', []))}")
        txs = get(f"https://api.hiro.so/extended/v1/address/{SENDER}/transactions?limit=1")
        last = (txs.get("results") or [{}])[0]
        print(f"  last confirmed: nonce={last.get('nonce')} status={last.get('tx_status')} at {last.get('burn_block_time_iso', '')[:19]}")
    except Exception as e:
        print(f"  hiro fetch failed: {e}")


if __name__ == "__main__":
    main()
