import cairosvg,math
W,H=1600,1000
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,40,"Hydroloop V14 MAX, MAX 2 and MAX 3: top view of a MAX 3, 240 wind turbines on 90 km of ridge and a single plant in the middle",21,"middle","bold","#6b5330"))
K=15.0
X0,YC=125,250
s.append(f'<rect x="20" y="80" width="{W-40}" height="350" fill="#d8b98a"/>')
def yc(km):
    return YC+18*math.sin(km/7.0)+10*math.sin(km/2.3)
pts=" ".join(f"{X0+km*K:.0f},{yc(km):.0f}" for km in [i*0.5 for i in range(181)])
s.append(f'<polyline points="{pts}" fill="none" stroke="#8a6a3e" stroke-width="10" stroke-linecap="round"/>')
s.append(txt(X0-10,yc(0)+5,"ridge",13,"end","bold","#5a4630"))
cols=["#5b9bd8","#3d82c9","#2f6fb5","#2f6fb5","#3d82c9","#5b9bd8"]
for g in range(6):
    for i in range(40):
        km=g*15+0.19+i*15/40
        x,y=X0+km*K,yc(km)
        s.append(f'<circle cx="{x:.1f}" cy="{y-14:.1f}" r="3.2" fill="{cols[g]}"/>')
    s.append(txt(X0+(g*15+7.5)*K,yc(g*15+7.5)-34,f"Group {g+1}: 40 x 6 MW",13,"middle","bold","#1b3a5c"))
xm=X0+45*K
s.append(f'<polyline points="{" ".join(f"{X0+km*K:.0f},{yc(km)+16:.0f}" for km in [i*0.5 for i in range(181)])}" fill="none" stroke="#333" stroke-width="2.5" stroke-dasharray="8,4"/>')
s.append(txt(X0+20*K,yc(20)+40,"225 kV backbone along the ridge",13,"middle","normal","#333"))
s.append(f'<rect x="{xm-9:.0f}" y="{yc(45)+20:.0f}" width="18" height="18" fill="#6b4f2e"/>')
s.append(f'<rect x="{xm-9:.0f}" y="{yc(45)+80:.0f}" width="18" height="18" fill="#777"/>')
s.append(f'<line x1="{xm:.0f}" y1="{yc(45)+38:.0f}" x2="{xm:.0f}" y2="{yc(45)+80:.0f}" stroke="#333" stroke-width="2.5"/>')
s.append(f'<line x1="{xm:.0f}" y1="{yc(45)+98:.0f}" x2="{xm:.0f}" y2="425" stroke="#333" stroke-width="2.5"/>')
s.append(txt(xm+16,yc(45)+34,"plant",13,"start","bold"))
s.append(txt(xm+16,yc(45)+94,"400 kV substation, 1,440 MW, to the grid",13,"start","bold"))
def cadre(a,b,d,col,lab):
    x1,x2=X0+a*15*K-d,X0+b*15*K+d
    s.append(f'<rect x="{x1:.0f}" y="{150-d:.0f}" width="{x2-x1:.0f}" height="{205+2*d:.0f}" fill="none" stroke="{col}" stroke-width="3" stroke-dasharray="12,6" rx="8"/>')
leg=[(2,4,6,"#b03a2e","MAX: groups 3 and 4, 30 km of ridge"),(1,5,18,"#7d3c98","MAX 2: groups 2 to 5, 60 km"),(0,6,30,"#1e6b3a","MAX 3: the six groups, 90 km")]
for i,(a,b,d,col,lab) in enumerate(leg):
    cadre(a,b,d,col,lab)
    lx=40+i*400
    s.append(f'<line x1="{lx}" y1="408" x2="{lx+40}" y2="408" stroke="{col}" stroke-width="3" stroke-dasharray="12,6"/>')
    s.append(txt(lx+50,413,lab,14,"start","bold",col))
sx,sy=1250,392
for i in range(2):
    s.append(f'<rect x="{sx+i*10*K:.0f}" y="{sy}" width="{10*K:.0f}" height="9" fill="{"#222" if i%2==0 else "#fff"}" stroke="#222"/>')
for i in range(3):
    s.append(txt(sx+i*10*K,sy+28,f"{i*10} km",12))
IX,IY,IW,IH=700,470,880,430
s.append(f'<rect x="{IX}" y="{IY}" width="{IW}" height="{IH}" fill="#e3cfa6" stroke="#8b6b3e" stroke-width="2"/>')
s.append(f'<line x1="{xm:.0f}" y1="{yc(45)+20:.0f}" x2="{IX+IW/2:.0f}" y2="{IY}" stroke="#8b6b3e" stroke-width="1.5" stroke-dasharray="4,4"/>')
s.append(txt(IX+IW/2,IY+24,"The hydraulic site in the middle of the ridge, enlarged (bottom ruler)",15,"middle","bold","#5a4630"))
Z=110.0
cy=IY+120
s.append(f'<rect x="{IX}" y="{cy-40}" width="{IW}" height="80" fill="#c9a676"/>')
s.append(txt(IX+12,cy-48,"ridge and upper basin",13,"start","bold","#5a4630"))
s.append(f'<rect x="{IX}" y="{cy+40}" width="{IW}" height="{1.2*Z:.0f}" fill="#d8b98a"/>')
for i in range(6):
    s.append(f'<line x1="{IX+10}" y1="{cy+52+i*22}" x2="{IX+IW-10}" y2="{cy+52+i*22}" stroke="#b7955f" stroke-width="2"/>')
s.append(txt(IX+12,cy+60,"300 m slope",13,"start","bold","#5a4630"))
s.append(f'<rect x="{IX}" y="{cy+40+1.2*Z:.0f}" width="{IW}" height="{IY+IH-(cy+40+1.2*Z):.0f}" fill="#e7d8b4"/>')
S=0.73*Z
bx=IX+IW/2-S/2
s.append(f'<rect x="{bx:.0f}" y="{cy-S/2:.0f}" width="{S:.0f}" height="{S:.0f}" fill="#1b4f7a"/>')
s.append(f'<rect x="{bx+S+10:.0f}" y="{cy-30:.0f}" width="182" height="40" rx="6" fill="#fff" fill-opacity="0.92"/>')
s.append(txt(bx+S+101,cy-14,"Upper basin 4.8 Mm³",13,"middle","bold"))
s.append(txt(bx+S+101,cy+2,"53 ha, open water",12,"middle"))
uy=cy+40+1.2*Z+10
for d in (-12,0,12):
    s.append(f'<line x1="{IX+IW/2+d:.0f}" y1="{cy+S/2:.0f}" x2="{IX+IW/2+d:.0f}" y2="{uy:.0f}" stroke="#2c3e50" stroke-width="5"/>')
s.append(f'<rect x="{IX+IW/2-60:.0f}" y="{cy+70:.0f}" width="120" height="40" rx="6" fill="#fff" fill-opacity="0.92"/>')
s.append(txt(IX+IW/2,cy+86,"3 penstocks",12,"middle","bold"))
s.append(txt(IX+IW/2,cy+102,"DN4000",12,"middle","bold"))
s.append(f'<rect x="{IX+IW/2-45:.0f}" y="{uy:.0f}" width="90" height="36" fill="#6b4f2e"/>')
s.append(txt(IX+IW/2,uy+15,"Plant",12,"middle","bold","#fff"))
s.append(txt(IX+IW/2,uy+30,"288 / 384 MW",11,"middle","normal","#fff"))
s.append(f'<rect x="{IX+IW/2+90:.0f}" y="{uy-4:.0f}" width="{S:.0f}" height="{S:.0f}" fill="#1b4f7a"/>')
s.append(txt(IX+IW/2+90+S/2,uy+S+12,"Lower basin 4.8 Mm³",13,"middle","bold"))
s.append(f'<rect x="{IX+IW/2-210:.0f}" y="{uy:.0f}" width="60" height="36" fill="#777"/>')
s.append(txt(IX+IW/2-180,uy+23,"Grid",12,"middle","bold","#fff"))
s.append(f'<line x1="{IX+IW/2-150:.0f}" y1="{uy+18:.0f}" x2="{IX+IW/2-45:.0f}" y2="{uy+18:.0f}" stroke="#333" stroke-width="2.5"/>')
s.append(f'<line x1="{IX+10}" y1="{cy-26}" x2="{bx-10:.0f}" y2="{cy-26}" stroke="#333" stroke-width="2.5" stroke-dasharray="8,4"/>')
s.append(f'<line x1="{bx+S+10:.0f}" y1="{cy+28}" x2="{IX+IW-10}" y2="{cy+28}" stroke="#333" stroke-width="2.5" stroke-dasharray="8,4"/>')
s.append(txt(IX+20,cy-8,"backbone, groups 1 to 3",12,"start","normal","#333"))
s.append(txt(IX+IW-20,cy+22,"backbone, groups 4 to 6",12,"end","normal","#333"))
zx,zy=IX+30,IY+IH-30
for i in range(2):
    s.append(f'<rect x="{zx+i*Z/2:.0f}" y="{zy}" width="{Z/2:.0f}" height="9" fill="{"#222" if i==0 else "#fff"}" stroke="#222"/>')
for i,l in enumerate(["0","500 m","1 km"]):
    s.append(txt(zx+i*Z/2,zy-6,l,12))
TX,TY=20,470
s.append(f'<rect x="{TX}" y="{TY}" width="660" height="430" fill="#fff" stroke="#8b6b3e" stroke-width="2"/>')
rows=[("","V14","MAX","MAX 2","MAX 3"),("6 MW wind turbines","40","80","160","240"),("Ridge (km)","15","30","60","90"),("Storage (MWh)","588","1,176","2,352","3,528"),("Connection (MW)","240","480","960","1,440"),("Sold at 6.7 m/s (GWh/yr)","886","1,765","3,506","5,224"),("With the line (M€)","385","743","1,435","2,112"),("M€ per annual GWh","0.435","0.421","0.409","0.404"),("Private LCOE (€/MWh)","28.4","27.4","26.7","26.4"),("Public LCOE (€/MWh)","23.9","23.0","22.3","22.0")]
for i,r in enumerate(rows):
    y=TY+34+i*32
    if i%2==1:
        s.append(f'<rect x="{TX+4}" y="{y-21}" width="652" height="32" fill="#f3ead9"/>')
    s.append(txt(TX+12,y,r[0],14,"start","bold" if i==0 else "normal"))
    for j,v in enumerate(r[1:]):
        s.append(txt(TX+330+j*90,y,v,14,"middle","bold" if i in (0,7) else "normal"))
yb=TY+34+len(rows)*32+8
for t in ["The wind turbines multiply: they make 63% of the price. The hydraulic site grows","more slowly, but a 15 km backbone per extra group is added, and the substation","was already shared in a cluster: 7% less per kWh in MAX 3."]:
    s.append(txt(TX+12,yb,t,13,"start"))
    yb+=21
s.append(f'<rect x="0" y="{H-70}" width="{W}" height="70" fill="#5a4630"/>')
s.append(txt(W/2,H-30,"Top view to scale (km ruler at the top, ruler of the enlargement at the bottom), reference wind of 6.7 m/s at the hub. 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_max.png")
