#!/usr/bin/env python3
"""Render the share card for the poidh personality tester.

Every figure is read out of index.json at render time, so the card cannot say
something the dataset does not.

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

HERE = os.path.dirname(os.path.abspath(__file__))
D = json.load(open(f"{HERE}/index.json"))
e = html.escape

W, H = 1200, 630
types = sorted(D["types"].items(), key=lambda kv: -kv[1]["n"])
assert len(types) == 16

s = [f'<svg xmlns="http://www.w3.org/2000/svg" width="{W}" height="{H}" viewBox="0 0 {W} {H}">',
     f'<rect width="{W}" height="{H}" fill="#0b0f14"/>',
     '<text x="60" y="82" fill="#e6edf3" font-size="42" font-weight="bold">'
     'poidh personality type</text>',
     '<text x="60" y="124" fill="#94a3b8" font-size="22">'
     f'{D["n_wallets"]:,} wallets &#183; {D["n_bounties"]:,} bounties &#183; '
     f'{D["n_claims"]:,} claims &#183; v1 + v2 + v3, four chains</text>']

# 4x4 grid of the sixteen types
CW, CH, GX, GY = 262, 92, 60, 160
for i, (code, t) in enumerate(types):
    col, row = i % 4, i // 4
    x, y = GX + col * (CW + 12), GY + row * (CH + 10)
    s += [f'<rect x="{x}" y="{y}" width="{CW}" height="{CH}" rx="11" '
          f'fill="#121821" stroke="#22303f"/>',
          f'<text x="{x+18}" y="{y+35}" fill="#7dd3fc" font-size="23" '
          f'font-weight="bold" font-family="monospace" letter-spacing="1.5">{code}</text>',
          f'<text x="{x+18}" y="{y+60}" fill="#e6edf3" font-size="17">'
          f'{e(t["name"])}</text>',
          f'<text x="{x+18}" y="{y+80}" fill="#94a3b8" font-size="14">'
          f'{t["n"]:,} wallet{"" if t["n"] == 1 else "s"} '
          f'&#183; {100.0*t["n"]/D["n_wallets"]:.1f}%</text>']

s += ['<text x="60" y="590" fill="#94a3b8" font-size="20">'
      'agentatwork.xyz/poidh-type &#183; enter a wallet or a Farcaster username</text>',
      '</svg>']

open(f"{HERE}/card.svg", "w").write("\n".join(s))
print("wrote card.svg")
