#!/usr/bin/env python3
"""Inspect Vigil's live x402 terms without authorizing or sending payment."""

import json
import uuid
import urllib.error
import urllib.request

URL = "https://vigilnotary.com/api/agent/v1/verify-source"
EXPECTED_RECIPIENT = "0xa4f4fe1d36eae9c7aadf2ea269b786549a27e523"
EXPECTED_BASE_USDC = "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913"

payload = {
    "source_url": "https://example.com",
    "claim": "This domain can be used in documentation examples without needing permission.",
    "options": {"max_age_seconds": 0, "max_price_usd": "0.03"},
}
request = urllib.request.Request(
    URL,
    data=json.dumps(payload).encode(),
    method="POST",
    headers={
        "Content-Type": "application/json",
        "Accept": "application/json",
        "Idempotency-Key": f"example-{uuid.uuid4()}",
    },
)

try:
    urllib.request.urlopen(request, timeout=20)
    raise SystemExit("Expected HTTP 402; the endpoint did not quote a payment.")
except urllib.error.HTTPError as exc:
    if exc.code != 402:
        raise
    challenge = json.load(exc)

quote = challenge["quote"]
base = next(x for x in challenge["payment_required"]["accepts"] if x["network"] == "eip155:8453")
assert quote["payment_mode"] == "mainnet"
assert quote["price_usd"] == "0.03" and quote["amount_atomic"] == "30000"
assert quote["resource_url"] == URL
assert base["scheme"] == "exact" and base["amount"] == "30000"
assert base["asset"].lower() == EXPECTED_BASE_USDC.lower()
assert base["payTo"].lower() == EXPECTED_RECIPIENT.lower()

print(json.dumps({"quote": quote, "base_payment": base}, indent=2))
print("\nTerms matched. No payment was authorized or sent.")
