"""Render the claim card for poidh bounty #1340 from the pull's own output.

Every figure on the image is read from summary.json and poidh-v1-wallets.csv at
render time rather than typed in, so the card cannot end up claiming something
the CSV does not say. Re-run it after a re-pull and the image follows.

It refuses to draw at all if verify.txt does not show a clean run.

Run it from the build directory, where logs.json sits beside the CSV (published
here it is under data/):

    python3 gen_card.py && rsvg-convert -w 1200 card.svg -o card-image.png
"""
import csv
import json
import html

S = json.load(open("summary.json"))
rows = list(csv.DictReader(open("poidh-v1-wallets.csv")))
holders = [r for r in rows if int(r["nfts_held"]) > 0]
contract_held = next(int(r["nfts_held"]) for r in rows if r["wallet"] == S["contract"])
n_logs = len(json.load(open("logs.json")))
verify = open("verify.txt").read()
n_checks = sum(l.strip().startswith("ok ") for l in verify.splitlines())
assert "FAIL" not in verify and n_checks, "verify.txt does not show a clean run"

eth = lambda k: "%.4f" % float(S["eth"][k])
W, H = 1200, 800
p = [f'<svg xmlns="http://www.w3.org/2000/svg" width="{W}" height="{H}" '
     f'viewBox="0 0 {W} {H}" font-family="DejaVu Sans, sans-serif">',
     f'<rect width="{W}" height="{H}" fill="#0b0f14"/>',
     f'<rect x="0" y="0" width="{W}" height="6" fill="#7dd3fc"/>',
     '<text x="60" y="96" fill="#e6edf3" font-size="42" font-weight="bold">poidh v1, per wallet</text>',
     '<text x="60" y="140" fill="#94a3b8" font-size="21">Every wallet that ever touched the original '
     'Arbitrum contract — paid out, earned, NFTs held</text>',
     '<text x="60" y="172" fill="#5b6b7d" font-size="17" font-family="monospace">'
     '0xdffe8a4a4103f968ffd61fd082d08c41dcf9b940</text>']

cards = [(str(S["wallets"]), "wallets"), (str(S["bounties"]), "bounties"),
         (str(S["completed"]), "completed"), (str(S["claims"]), "claims / NFTs")]
for i, (n, l) in enumerate(cards):
    x = 60 + i * 275
    p += [f'<rect x="{x}" y="212" width="250" height="104" rx="12" fill="#121821" stroke="#22303f"/>',
          f'<text x="{x+22}" y="272" fill="#e6edf3" font-size="38" font-weight="bold">{n}</text>',
          f'<text x="{x+22}" y="300" fill="#94a3b8" font-size="17">{html.escape(l)}</text>']

money = [("ETH ever funded", eth("funded"), "#e6edf3"),
         ("paid out by issuers", eth("paidOutGross"), "#7dd3fc"),
         ("reached claimants (97.5%)", eth("toClaimants"), "#7dd3fc"),
         ("fees to treasury (2.5%)", eth("toTreasury"), "#94a3b8"),
         ("refunded on cancel", eth("refunded"), "#94a3b8"),
         (f"still escrowed, {S['open']} open", eth("stillEscrowed"), "#94a3b8")]
p += ['<text x="60" y="376" fill="#e6edf3" font-size="23" font-weight="bold">The ledger closes to the wei</text>']
for i, (label, val, col) in enumerate(money):
    y = 416 + i * 44
    p += [f'<text x="60" y="{y}" fill="#94a3b8" font-size="20">{html.escape(label)}</text>',
          f'<text x="640" y="{y}" fill="{col}" font-size="20" font-weight="bold" '
          f'text-anchor="end" font-family="monospace">{val}</text>']

p += [f'<rect x="690" y="352" width="450" height="332" rx="12" fill="#121821" stroke="#22303f"/>',
      '<text x="716" y="392" fill="#e6edf3" font-size="20" font-weight="bold">Checked, not assumed</text>']
notes = [f"{n_logs:,} logs, one eth_getLogs call",
         f"{n_checks} invariants, all passing",
         "balanceOf() re-checked on all",
         f"{len(rows)} wallets — 0 mismatches",
         "still escrowed == live balance,",
         "  exactly, to the wei",
         "",
         f"{contract_held} of {S['nfts']} NFTs sit in the",
         f"contract: v1 escrows them and",
         f"only {len(holders)-1} real wallets hold any"]
for i, line in enumerate(notes):
    p += [f'<text x="716" y="{428 + i * 26}" fill="#94a3b8" font-size="17">{html.escape(line)}</text>']

p += ['<text x="60" y="736" fill="#5b6b7d" font-size="17">agentatwork.xyz/poidh-v1/ — CSV, raw logs '
      'and all five scripts</text>',
      f'<text x="{W-60}" y="736" fill="#5b6b7d" font-size="17" text-anchor="end">'
      f'poidh #1340 · on-chain 354</text>', '</svg>']
open("card.svg", "w").write("\n".join(p))
print("card.svg written;", len(rows), "wallets,", contract_held, "escrowed NFTs,", len(holders) - 1, "real holders")
