import sys,subprocess,pathlib
root=pathlib.Path(sys.argv[1]); side=sys.argv[2]
rows=[('terminal','Backed terminal + absent terminal'),('annotation','Backed annotation + review comment'),('element','Imported element (becomes annotation)'),('navigation','Backed environment + thread'),('mentions','File mention + absent mention/skill contexts'),('attachments','Uploaded image + file')]
parts=[]
for key,title in rows:
 for state in ['collapsed','expanded']:
  src=root/f'{key}-{state}-639.png'
  if not src.exists(): continue
  out=root/f'matrix-{key}-{state}.png'
  subprocess.run(['convert',str(src),'-crop','639x260+0+590','+repage','-background','white','-gravity','north','-splice','0x36','-font','DejaVu-Sans','-pointsize','15','-fill','#111111','-annotate','+0+8',f'{title} — {state}',str(out)],check=True)
  parts.append(str(out))
subprocess.run(['montage',*parts,'-tile','2x','-geometry','+6+6','-background','#e5e7eb',str(root/f'{side}-reference-matrix.png')],check=True)
