"""Charts for the invoice-extraction lab. Usage: python3 charts.py <out_dir>  (reads summary.json and summary-b.json beside it)"""
import json, os, sys
OUT = sys.argv[1]; H = os.path.dirname(os.path.abspath(__file__))
A = json.load(open(H + '/summary.json')); B = json.load(open(H + '/summary-b.json'))
NAME = {'gemini-4-hydrogen': 'Gemini 4 Hydrogen', 'claude-sonnet-5-5': 'Claude Sonnet 5.5', 'gpt-6.2-sol': 'GPT-6.2 Sol', 'claude-haiku-5-5': 'Claude Haiku 5.5', 'gpt-6-luna': 'GPT-6 Luna', 'gemini-3.5-flash-lite': 'Gemini 3.5 Flash-Lite'}
F = 'font-family="IBM Plex Sans, Inter, system-ui, sans-serif"'
TEAL, TERRA, S6, S5, S2, W = '#0e7c7b', '#b05525', '#475569', '#64748b', '#e2e8f0', '#ffffff'
def svg(title, desc, body, h=630):
    return (f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1200 {h}" width="100%" role="img" aria-labelledby="t d"><title id="t">{title}</title><desc id="d">{desc}</desc>'
            f'<rect width="1200" height="{h}" rx="12" fill="{W}"/>{body}</svg>\n')
def txt(x, y, s, size=24, fill=S6, weight='400', anchor='start'):
    return f'<text x="{x}" y="{y}" {F} font-size="{size}" font-weight="{weight}" fill="{fill}" text-anchor="{anchor}">{s}</text>'
order = sorted(NAME, key=lambda m: -B[m]['perfect_docs'])
# 1. fully correct documents, ordinary vs hard
b = txt(48, 66, 'Documents with every value right, by model', 32, S6, '600')
b += f'<rect x="48" y="92" width="22" height="22" rx="4" fill="{S5}"/>' + txt(80, 111, 'Ordinary set (60 documents)', 21) + f'<rect x="400" y="92" width="22" height="22" rx="4" fill="{TEAL}"/>' + txt(432, 111, 'Hard set (40 invoices)', 21)
x0, bw = 330, 720
for i, m in enumerate(order):
    y = 138 + i * 76; b += txt(48, y + 44, NAME[m], 24)
    for k, (v, fill) in enumerate(((A[m]['perfect_docs'], S5), (B[m]['perfect_docs'], TEAL))):
        yy = y + k * 34; L = bw * v / 100
        b += f'<rect x="{x0}" y="{yy}" width="{L:.1f}" height="28" rx="4" fill="{fill}"/>' + txt(x0 + L + 12, yy + 21, f'{v:g}%', 21, S6, '600')
b += txt(48, 610, 'Two passes per model. Pondero Labs, December 1, 2026.', 19, S5)
open(OUT + '/fully-correct-by-model.svg', 'w').write(svg('Fully correct documents by model', '; '.join(f"{NAME[m]}: ordinary {A[m]['perfect_docs']}%, hard {B[m]['perfect_docs']}%" for m in order), b))
# 2. hard set by invoice family
fam = [('perfect_rental', 'Rental'), ('perfect_paint', 'Paint'), ('perfect_screening', 'Screening'), ('perfect_scan', 'Scanned (any type)')]
b = txt(48, 66, 'Hard set: fully correct invoices by type', 32, S6, '600')
cw = 205
for j, (k, lab) in enumerate(fam): b += txt(330 + j * cw + cw / 2, 118, lab, 21, S6, '600', 'middle')
for i, m in enumerate(order):
    y = 132 + i * 76; b += txt(48, y + 46, NAME[m], 24)
    for j, (k, lab) in enumerate(fam):
        v = B[m][k]; x = 330 + j * cw + 14; L = (cw - 28) * v / 100
        b += f'<rect x="{x}" y="{y+14}" width="{cw-28}" height="44" rx="4" fill="{S2}"/><rect x="{x}" y="{y+14}" width="{max(L,2):.1f}" height="44" rx="4" fill="{TERRA if v < 60 else TEAL}"/>' + (txt(x + L / 2, y + 44, f'{v:g}%', 22, W, '600', 'middle') if v > 45 else txt(x + cw - 38, y + 44, f'{v:g}%', 22, S6, '600', 'end'))
b += txt(48, 604, '18 rental, 12 paint and 10 screening invoices; 13 of the 40 are degraded to look like scans. Two passes per model.', 19, S5)
open(OUT + '/hard-set-by-type.svg', 'w').write(svg('Hard set results by invoice type', '; '.join(f"{NAME[m]}: " + ', '.join(f'{lab} {B[m][k]}%' for k, lab in fam) for m in order), b))
# 3. cost per 1,000 hard invoices
mx = max(B[m]['cost_per_1000_docs'] for m in order)
b = txt(48, 66, 'Cost per 1,000 hard invoices at list price (USD)', 32, S6, '600')
for i, m in enumerate(order):
    y = 104 + i * 80; v = B[m]['cost_per_1000_docs']; L = 640 * v / mx
    b += txt(48, y + 40, NAME[m], 24) + f'<rect x="330" y="{y+8}" width="{max(L,3):.1f}" height="46" rx="4" fill="{TEAL}"/>' + txt(330 + L + 14, y + 40, f'${v:.2f}', 22, S6, '600') + txt(1150, y + 40, f"{B[m]['perfect_docs']:g}% right", 20, S5, '400', 'end')
b += txt(48, 604, 'From measured token use and each vendor\'s standard list price on December 1, 2026.', 19, S5)
open(OUT + '/cost-per-1000.svg', 'w').write(svg('Cost per 1,000 hard invoices', '; '.join(f"{NAME[m]} ${B[m]['cost_per_1000_docs']:.2f}" for m in order), b))
