from pathlib import Path
import csv
out=Path(__file__).resolve().parent
AU_PER_LY=63241.077
D_KM=13000.0
TARGET_LY=100.0
rows=[]
for au in range(550,1001,25):
    rows.append((au,D_KM*au/(TARGET_LY*AU_PER_LY)))
with (out/'image-region-scale.csv').open('w',newline='',encoding='utf-8') as f:
    w=csv.writer(f);w.writerow(['observer_distance_AU','compressed_region_km','target_diameter_km','target_distance_ly','AU_per_light_year','classification'])
    for au,km in rows:w.writerow([au,f'{km:.6f}',D_KM,TARGET_LY,AU_PER_LY,'simple geometry calculation; not measured; not resolution'])
def make_svg(width,height,mobile=False):
    left,right=(80,18) if mobile else (96,28);top,bottom=30,(94 if mobile else 76)
    pw=width-left-right;ph=height-top-bottom;xmin,xmax=550,1000;ymin,ymax=1.0,2.2
    X=lambda x:left+(x-xmin)/(xmax-xmin)*pw
    Y=lambda y:top+(ymax-y)/(ymax-ymin)*ph
    font=23 if mobile else 16
    path=' '.join(('M' if i==0 else 'L')+f'{X(a):.1f},{Y(b):.1f}' for i,(a,b) in enumerate(rows))
    svg=[f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {width} {height}" role="img" aria-labelledby="t d"><title id="t">Calculated size of the compressed target region</title><desc id="d">For a 13,000 kilometre target at 100 light-years, a simple geometric estimate grows from about 1.13 kilometres at 550 astronomical units to 2.05 kilometres at 1,000 astronomical units. It is not optical resolution or a reconstructed image.</desc><rect width="100%" height="100%" fill="#fff"/>']
    for y in [1.0,1.2,1.4,1.6,1.8,2.0,2.2]:
        yy=Y(y);svg.append(f'<path d="M{left},{yy:.1f}H{width-right}" stroke="#d5deeb"/><text x="{left-9}" y="{yy+5:.1f}" text-anchor="end" font-size="{font}" fill="#172a46">{y:.1f}</text>')
    for x in [550,650,750,850,1000]:
        xx=X(x);svg.append(f'<path d="M{xx:.1f},{top}V{height-bottom}" stroke="#edf1f6"/><text x="{xx:.1f}" y="{height-bottom+24}" text-anchor="middle" font-size="{font}" fill="#172a46">{x}</text>')
    svg += [f'<path d="M{left},{top}V{height-bottom}H{width-right}" fill="none" stroke="#172a46" stroke-width="2"/>',f'<path d="{path}" fill="none" stroke="#176a8a" stroke-width="4"/>']
    xx=X(650);yy=Y(D_KM*650/(TARGET_LY*AU_PER_LY))
    svg += [f'<circle cx="{xx:.1f}" cy="{yy:.1f}" r="6" fill="#c14e35"/>',f'<text x="{xx+8:.1f}" y="{yy-12:.1f}" font-size="{font}" fill="#713126">~1.34 km at 650 AU</text>',f'<text x="{left+pw/2:.1f}" y="{height-17}" text-anchor="middle" font-size="{font}" fill="#172a46">Observer distance from Sun (AU)</text>',f'<text transform="translate(24 {top+ph/2:.1f}) rotate(-90)" text-anchor="middle" font-size="{font}" fill="#172a46">Geometric region width (km)</text>',f'<text x="{left}" y="{height-43}" font-size="{font-2}" fill="#44546b">Simple geometry only · not optical resolution</text></svg>']
    return '\n'.join(svg)
(out/'region-scale.svg').write_text(make_svg(1100,650),encoding='utf-8')
def make_mobile_svg():
    # Phone typography at 310 CSS px: 20 SVG units render as 15.5 CSS px.
    width, height = 400, 540
    left, right, top, bottom = 62, 45, 58, 365
    X = lambda au: left + (au-550)/450*(width-left-right)
    Y = lambda km: top + (2.2-km)/1.2*(bottom-top)
    parts = [f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {width} {height}" role="img" aria-labelledby="t d" font-family="Arial, sans-serif"><title id="t">Calculated compressed target region</title><desc id="d">Same geometric calculation as the wide graph: a 13,000 kilometre target at 100 light-years spans about 1.13 to 2.05 kilometres as observer distance increases from 550 to 1,000 AU. This is not optical resolution or a measured image.</desc><rect width="100%" height="100%" fill="white"/><g fill="#172a46" font-size="20"><text x="16" y="28">Geometric region width (km)</text>']
    for km in [1.0, 1.4, 1.8, 2.2]:
        y=Y(km)
        parts.append(f'<path d="M{left},{y:.2f}H{width-right}" fill="none" stroke="#d5deeb"/><text x="{left-10}" y="{y+7:.2f}" text-anchor="end">{km:.1f}</text>')
    for au in [550, 750, 1000]:
        x=X(au)
        parts.append(f'<path d="M{x:.2f},{top}V{bottom}" fill="none" stroke="#edf1f6"/><text x="{x:.2f}" y="{bottom+29}" text-anchor="middle">{au}</text>')
    curve=' '.join(('M' if i==0 else 'L')+f'{X(au):.2f},{Y(km):.2f}' for i,(au,km) in enumerate(rows))
    x,y=X(650),Y(D_KM*650/(TARGET_LY*AU_PER_LY))
    parts.extend([f'<path d="M{left},{top}V{bottom}H{width-right}" stroke="#172a46" fill="none"/><path d="{curve}" fill="none" stroke="#176a8a" stroke-width="3"/>',f'<circle cx="{x:.2f}" cy="{y:.2f}" r="4" fill="#a7402b"/><text x="{x+12:.2f}" y="{y+15:.2f}" fill="#713126">1.34 km</text><text x="{x+12:.2f}" y="{y+40:.2f}" fill="#713126">at 650 AU</text>','<text x="200" y="430" text-anchor="middle">Observer distance from Sun</text><text x="200" y="456" text-anchor="middle">(astronomical units)</text><text x="16" y="492">Simple geometry only.</text><text x="16" y="516">Not optical resolution.</text></g></svg>'])
    return '\n'.join(parts)

(out/'region-scale-mobile.svg').write_text(make_mobile_svg(),encoding='utf-8')
