import cairosvg
W,H=1600,1000
K=90.0
def txt(x,y,s,size=15,anchor="middle",weight="normal",fill="#111"):
    return f'<text x="{x:.0f}" y="{y:.0f}" font-family="Arial,Helvetica,sans-serif" font-size="{size}" text-anchor="{anchor}" font-weight="{weight}" fill="{fill}">{s}</text>'
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="#f6efe2"/>']
s.append(txt(W/2,42,"Hydroloop V14 Soleil MAX, MAX 2 and MAX 3: top view of a MAX 3, six V14 Soleil joined on a single plateau",22,"middle","bold","#6b5330"))
X0,XV,XP=430,640,748
Y0,Y1=110,860
s.append(f'<rect x="{X0}" y="{Y0}" width="{XV-X0}" height="{Y1-Y0}" fill="#e3cfa6"/>')
s.append(f'<rect x="{XV}" y="{Y0}" width="{XP-XV}" height="{Y1-Y0}" fill="#c9a676"/>')
for i in range(11):
    y=Y0+70+i*62
    s.append(f'<line x1="{XV+8}" y1="{y}" x2="{XP-8}" y2="{y}" stroke="#a9865a" stroke-width="2"/>')
s.append(f'<rect x="{XP}" y="{Y0}" width="{W-XP}" height="{Y1-Y0}" fill="#d8b98a"/>')
s.append(txt((X0+XV)/2,Y0+24,"Plain",15,"middle","bold","#5a4630"))
s.append(txt((XV+XP)/2,Y0+24,"Slope",15,"middle","bold","#5a4630"))
s.append(txt((XV+XP)/2,Y0+42,"300 m",13,"middle","normal","#5a4630"))
s.append(txt(1180,Y0+24,"Sunny plateau: 42 km² of bifacial trackers",15,"middle","bold","#5a4630"))
B=2.65*K
FX,FY=830,215
cols=[("#2f6fb5",1),("#2f6fb5",2),("#3d82c9",3),("#3d82c9",4),("#5b9bd8",5),("#5b9bd8",6)]
for i in range(6):
    c,r=i//2,i%2
    x,y=FX+c*B,FY+r*B
    col=cols[i][0]
    s.append(f'<rect x="{x+4:.0f}" y="{y+4:.0f}" width="{B-8:.0f}" height="{B-8:.0f}" fill="{col}" fill-opacity="0.18" stroke="{col}" stroke-width="1.5"/>')
    for j in range(16):
        yy=y+14+j*(B-24)/15
        s.append(f'<line x1="{x+14:.0f}" y1="{yy:.0f}" x2="{x+B-14:.0f}" y2="{yy:.0f}" stroke="{col}" stroke-width="3"/>')
    s.append(f'<rect x="{x+B/2-70:.0f}" y="{y+B/2-24:.0f}" width="140" height="44" rx="6" fill="#ffffff" fill-opacity="0.9"/>')
    s.append(txt(x+B/2,y+B/2-4,f"Block {i+1}",15,"middle","bold","#1b3a5c"))
    s.append(txt(x+B/2,y+B/2+14,"500 MWp, 7 km²",13,"middle","normal","#1b3a5c"))
def cadre(n,col,lab,d):
    w=(n//2)*B
    s.append(f'<rect x="{FX-d:.0f}" y="{FY-d:.0f}" width="{w+2*d:.0f}" height="{2*B+2*d:.0f}" fill="none" stroke="{col}" stroke-width="3" stroke-dasharray="12,6" rx="8"/>')
leg=[("#b03a2e","MAX: blocks 1 and 2",8,2),("#7d3c98","MAX 2: blocks 1 to 4",22,4),("#1e6b3a","MAX 3: the six blocks",36,6)]
for i,(col,lab,d,n) in enumerate(leg):
    cadre(n,col,lab,d)
    lx0=860+i*240
    s.append(f'<line x1="{lx0}" y1="770" x2="{lx0+40}" y2="770" stroke="{col}" stroke-width="3" stroke-dasharray="12,6"/>')
    s.append(txt(lx0+50,775,lab,14,"start","bold",col))
BH=0.73*K
bhx,bhy=XP+12,FY+B-BH/2
s.append(f'<rect x="{bhx:.0f}" y="{bhy:.0f}" width="{BH:.0f}" height="{BH:.0f}" fill="#1b4f7a"/>')
for j in range(5):
    s.append(f'<rect x="{bhx+6:.0f}" y="{bhy+6+j*11:.0f}" width="{BH-12:.0f}" height="6" fill="#2f6fb5"/>')
s.append(f'<rect x="{bhx+BH/2-62:.0f}" y="{bhy-52:.0f}" width="124" height="44" rx="6" fill="#ffffff" fill-opacity="0.92"/>')
s.append(txt(bhx+BH/2,bhy-34,"Upper basin",14,"middle","bold"))
s.append(txt(bhx+BH/2,bhy-16,"4.8 Mm³, 53 ha",12,"middle"))
bbx,bby=XV-12-BH,FY+B+60
s.append(f'<rect x="{bbx:.0f}" y="{bby:.0f}" width="{BH:.0f}" height="{BH:.0f}" fill="#1b4f7a"/>')
for j in range(5):
    s.append(f'<rect x="{bbx+6:.0f}" y="{bby+6+j*11:.0f}" width="{BH-12:.0f}" height="6" fill="#2f6fb5"/>')
s.append(txt(bbx+BH/2,bby+BH+20,"Lower basin",14,"middle","bold"))
s.append(txt(bbx+BH/2,bby+BH+38,"4.8 Mm³, 53 ha",12,"middle"))
ux,uy=XV-50,FY+B-10
s.append(f'<rect x="{ux}" y="{uy}" width="44" height="40" fill="#6b4f2e"/>')
s.append(txt(ux+22,uy+25,"Plant",11,"middle","bold","#fff"))
for d in (-8,0,8):
    s.append(f'<line x1="{bhx:.0f}" y1="{FY+B+d:.0f}" x2="{ux+44}" y2="{uy+20+d}" stroke="#2c3e50" stroke-width="4"/>')
s.append(f'<rect x="{(XV+XP)/2-46:.0f}" y="{FY+B+24:.0f}" width="92" height="40" rx="6" fill="#ffffff" fill-opacity="0.92"/>')
s.append(txt((XV+XP)/2,FY+B+40,"3 penstocks",12,"middle","bold"))
s.append(txt((XV+XP)/2,FY+B+56,"DN4000",12,"middle","bold"))
s.append(f'<line x1="{ux+22}" y1="{uy+40}" x2="{bbx+BH/2:.0f}" y2="{bby:.0f}" stroke="#2c3e50" stroke-width="4"/>')
px_,py_=X0+20,FY+B-30
s.append(f'<rect x="{px_}" y="{py_}" width="64" height="44" fill="#777"/>')
s.append(txt(px_+32,py_+27,"Grid",13,"middle","bold","#fff"))
s.append(f'<line x1="{px_+64}" y1="{py_+22}" x2="{ux}" y2="{uy+20}" stroke="#333" stroke-width="2.5"/>')
s.append(txt(px_+32,py_+66,"400 kV",12,"middle","bold"))
s.append(txt(px_+32,py_+82,"1,920 MW",12,"middle","bold"))
s.append(f'<line x1="{px_}" y1="{py_+22}" x2="{X0-6}" y2="{py_+22}" stroke="#333" stroke-width="2.5"/>')
s.append(txt(1180,FY+2*B+110,"Gutters on each row: rain and dew from the field flow down to the upper basin",13,"middle","normal","#3b2a14"))
sx,sy=830,Y1-24
for i in range(5):
    s.append(f'<rect x="{sx+i*K:.0f}" y="{sy}" width="{K:.0f}" height="10" fill="{"#222" if i%2==0 else "#fff"}" stroke="#222" stroke-width="1"/>')
for i in (0,1,2,3,4,5):
    s.append(txt(sx+i*K,sy+30,f"{i} km",13))
lx,ly=20,110
s.append(f'<rect x="{lx}" y="{ly}" width="395" height="750" fill="#fff" stroke="#8b6b3e" stroke-width="2"/>')
rows=[("","Base","MAX","MAX 2","MAX 3"),("Capacity","×1","×2","×4","×6"),("Trackers (MWp)","500","1,000","2,000","3,000"),("Field (km²)","7","14","28","42"),("Storage (MWh)","588","1,176","2,352","3,528"),("Connection (MW)","320","640","1,280","1,920"),("Sold (GWh/yr)","998","1,985","3,949","5,909"),("Investment (M€)","445","853","1,642","2,414"),("With the line (M€)","465","883","1,688","2,473"),("M€ per annual GWh","0.466","0.445","0.428","0.418"),("Private LCOE (€/MWh)","30.0","28.6","27.5","26.9"),("Public LCOE (€/MWh)","25.1","23.9","22.9","22.4"),("Payback (years)","10.2","9.7","9.3","9.1")]
for i,r in enumerate(rows):
    y=ly+38+i*34
    if i%2==1:
        s.append(f'<rect x="{lx+4}" y="{y-22}" width="387" height="34" fill="#f3ead9"/>')
    s.append(txt(lx+12,y,r[0],13,"start","bold" if i==0 else "normal"))
    for j,v in enumerate(r[1:]):
        s.append(txt(lx+218+j*50,y,v,13,"middle","bold" if i in (0,9) else "normal"))
yb=ly+38+len(rows)*34+10
for t in ["What multiplies: trackers, gutters.","What grows more slowly: basins (power 0.8),","penstocks (0.75), machines (0.9), substation (0.75),","line (0.6), floating and cover (area).","What is paid only once: slope, road,","studies, instrumentation.","Same control, same dispatchable share (18%)."]:
    s.append(txt(lx+12,yb,t,13,"start"))
    yb+=22
s.append(f'<rect x="0" y="{H-80}" width="{W}" height="80" fill="#5a4630"/>')
s.append(txt(W/2,H-35,"Top view to scale (km ruler), reference site with 2,100 tracker hours. www.mulo.world, October 2026, CC BY 4.0",14,"middle","normal","#f6efe2"))
s.append('</svg>')
cairosvg.svg2png(bytestring="\n".join(s).encode(),write_to="schema_hydroloop_muhleisen_2026_v14_soleil_max.png")
