"""Charts for the Lindy price-check lab. Usage: python3 charts.py <out_dir>"""
import sys
OUT=sys.argv[1]
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):
    return (f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1200 630" width="100%" role="img" aria-labelledby="t d">'
            f'<title id="t">{title}</title><desc id="d">{desc}</desc>'
            f'<defs><pattern id="hatch" width="12" height="12" patternUnits="userSpaceOnUse" patternTransform="rotate(45)">'
            f'<rect width="12" height="12" fill="{S2}"/><rect width="5" height="12" fill="{S5}"/></pattern></defs>'
            f'<rect width="1200" height="630" rx="12" fill="{W}"/>{body}</svg>\n')
def txt(x,y,s,size=26,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>'

# 1 accuracy by batch (stacked counts)
rows=[('Warm-up',10,0,3),('Batch A',30,1,0),('Batch B',8,1,3),('Batch C',17,0,0),('Batch D',10,3,0)]
x0,bw,maxv=200,760,31
b=txt(48,70,'Lindy price rows checked against vendor pages, by batch',32,S6,'600')
lx=48
for lab,fill in (('Correct',TEAL),('Partly right',"url(#hatch)"),('Wrong',TERRA)):
    b+=f'<rect x="{lx}" y="96" width="22" height="22" rx="4" fill="{fill}"/>'+txt(lx+32,116,lab,22)
    lx+=200
for i,(lab,c,p,w) in enumerate(rows):
    y=150+i*86; x=x0
    b+=txt(48,y+40,lab,26)
    for v,fill in ((c,TEAL),(p,"url(#hatch)"),(w,TERRA)):
        if not v: continue
        L=bw*v/maxv
        b+=f'<rect x="{x:.1f}" y="{y+8}" width="{L-2:.1f}" height="48" rx="4" fill="{fill}"/>'
        if fill==TEAL and L>60: b+=txt(x+14,y+41,str(v),24,W,'600')
        elif fill==TERRA: b+=txt(x+L/2-1,y+41,str(v),24,W,'600','middle')
        x+=L
    n=c+p+w
    b+=txt(x0+bw*n/maxv+16,y+41,f'{c} of {n} correct',22,S6)
b+=txt(48,596,'Warm-up: Cursor, Zapier, n8n. Batches A to D: 20 tools. 86 rows in all. Pondero Labs, October 19, 2026.',20,S5)
desc='; '.join(f'{l}: {c} correct, {p} partly right, {w} wrong' for l,c,p,w in rows)
open(f'{OUT}/accuracy-by-batch.svg','w').write(svg('Lindy pricing accuracy by batch',desc,b))

def hbars(fname,title,heading,vals,maxv,note):
    x0,bw=200,800
    b=txt(48,70,heading,32,S6,'600')
    for i,(lab,v,shown,hi) in enumerate(vals):
        y=110+i*(90 if len(vals)>3 else 150); L=bw*min(v,maxv)/maxv
        b+=txt(48,y+44,lab,26)
        b+=f'<rect x="{x0}" y="{y+12}" width="{bw}" height="48" rx="4" fill="{S2}"/>'
        b+=f'<rect x="{x0}" y="{y+12}" width="{max(L,6):.1f}" height="48" rx="4" fill="{TERRA if hi else TEAL}"/>'
        inside=L>300
        b+=txt(x0+L-16 if inside else x0+max(L,6)+14,y+45,shown,24,W if inside else S6,'600','end' if inside else 'start')
    b+=txt(48,596,note,20,S5)
    desc='; '.join(f'{l}: {s}' for l,_,s,_ in vals)
    open(f'{OUT}/{fname}','w').write(svg(title,desc,b))

hbars('time-per-batch.svg','Time Lindy took per pricing batch','Time to answer, per batch (seconds)',
      [('Batch C',14,'14 s',0),('Batch D',15,'15 s',0),('Warm-up',47,'about 47 s',0),('Batch B',66,'66 s',0),('Batch A',903,'more than 15 minutes',1)],960,
      'Batch A: GitHub Copilot, Claude, ChatGPT, Perplexity, Windsurf. Pondero Labs, October 19, 2026.')
hbars('credits-per-batch.svg','Lindy credits used per pricing batch','Lindy credits used, per batch (1 credit is about $0.01)',
      [('Warm-up',5,'about 5',0),('Batch B',16,'16',0),('Batch A',241,'241 (about $2.41)',1)],260,
      "Batches C and D used 6 or fewer each. From Lindy's usage page, October 19, 2026.")
