from PIL import Image,ImageDraw,ImageFont
from pathlib import Path
import json
R=Path(__file__).resolve().parent
font='/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf'
f=lambda n:ImageFont.truetype(font,n)
kit=json.loads((R/'kit.json').read_text());out=Image.new('RGB',(2400,1890),'#e6e7e3');d=ImageDraw.Draw(out)
d.text((54,34),'MODERN / CONSTRUCTION ATOMS',font=f(38),fill='#25382f');d.text((56,91),'Contemporary European residential architecture  /  19 masters  /  metres',font=f(22),fill='#5f6b63')
for i,a in enumerate(kit['atoms']):
 x=44+i%5*465;y=155+i//5*420
 im=Image.open(R/'cards'/(a['node_name']+'.png')).convert('RGB');im=im.resize((450,340),Image.Resampling.LANCZOS);out.paste(im,(x,y))
 d.text((x+10,y+349),a['node_name'],font=f(21),fill='#23392e');d.text((x+10,y+380),str(a['triangles'])+' / '+str(a['triangle_budget'])+' triangles',font=f(17),fill='#63716a')
x=44+4*465;y=155+3*420
d.text((x+18,y+36),'ASSEMBLY CONTRACT',font=f(22),fill='#23392e')
for j,line in enumerate(['Y up · +Z outward','0.35 m generated walls','3.00 m storey grid','Same tall window each storey','External PBR material shelf','Identity-transform GLB masters']):d.text((x+18,y+91+j*37),line,font=f(19),fill='#63716a')
d.text((55,1840),f"{kit['total_triangles']:,} triangles total  ·  Every atom under budget  ·  Source and joint rules included",font=f(21),fill='#4a5d52');out.save(R/'kit_sheet.png')
shelf=json.loads((R/'materials.json').read_text())['materials'];out=Image.new('RGB',(1900,1530),'#e6e7e3');d=ImageDraw.Draw(out);d.text((40,24),'MATERIAL SHELF / METRE-SCALE PBR',font=f(35),fill='#25382f')
for i,(name,spec) in enumerate(shelf.items()):
 x=35+i%4*470;y=100+i//4*465
 im=Image.open(R/spec['maps']['BaseColor']).resize((440,370),Image.Resampling.LANCZOS);out.paste(im,(x,y));d.text((x,y+382),name,font=f(22),fill='#23392e');d.text((x,y+417),f"{spec['tile_size_metres']} m repeat · {spec['resolution']} px"+(' · Height' if 'height_map' in spec else ''),font=f(18),fill='#63716a')
out.save(R/'material_sheet.png')
