#!/usr/bin/env python3
"""Rebuild the idealized AEPS-to-spacecraft example from NASA's paired test row."""
import csv, math
from pathlib import Path
HERE = Path(__file__).resolve().parent
with (HERE / "aeps-ppe-test-data.csv").open(newline="", encoding="utf-8") as f:
    row = next(r for r in csv.DictReader(f) if r["point_id"] == "8")
THRUST_N = float(row["thrust_mN"]) / 1000
ISP_S = float(row["specific_impulse_s"])
VE = ISP_S * 9.80665
MDOT = THRUST_N / VE
M0 = 1000.0
DAYS = (0, 1, 3, 7, 14, 21, 30)
with (HERE / "aeps-measured-mission-example.csv").open("w", newline="", encoding="utf-8") as f:
    w = csv.writer(f)
    w.writerow(["elapsed_days", "ideal_delta_v_m_s", "propellant_used_kg", "spacecraft_mass_kg", "assumed_constant_thrust_mN", "assumed_constant_specific_impulse_s", "assumed_discharge_power_kW", "initial_mass_kg", "evidence_status"])
    for day in DAYS:
        used = MDOT * day * 86400
        mass = M0 - used
        dv = VE * math.log(M0 / mass)
        w.writerow([day, round(dv, 3), round(used, 4), round(mass, 4), row["thrust_mN"], ISP_S, float(row["discharge_power_W"]) / 1000, M0, "calculated from NASA lab test point; hypothetical 1000 kg spacecraft; idealized"])
points=[]
for day in range(31):
    dv=VE*math.log(M0/(M0-MDOT*day*86400))
    points.append((90+day*25, 330-dv*.115, day, dv))
poly=" ".join(f"{x},{y:.2f}" for x,y,_,_ in points)
marks=[]
for x,y,day,dv in points:
    if day in (0,1,7,14,21,30):
        marks.append(f'<circle cx="{x}" cy="{y:.2f}" r="5" fill="#ffbf69"/><text x="{x}" y="{y-13:.2f}" text-anchor="middle" fill="#eef4ff" font-size="14">{dv:.0f}</text>')
svg=f'''<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1000 440" role="img" aria-labelledby="title desc"><title id="title">Idealized velocity change using one measured AEPS laboratory operating point</title><desc id="desc">Calculated delta-v rises to about 1.59 kilometers per second over 30 days for a hypothetical 1000 kilogram spacecraft, assuming constant 594 millinewton thrust and 2740 second specific impulse. This is not a flight prediction.</desc><rect width="1000" height="440" fill="#0e1c35"/><text x="500" y="36" text-anchor="middle" fill="#fff" font-size="22" font-family="system-ui">Ideal velocity change · illustrative 1,000 kg spacecraft</text><g stroke="#5a6e91" stroke-width="1"><path d="M90 60V330H840"/><path d="M90 250H840M90 170H840M90 90H840" opacity=".6"/></g><g fill="#dce8ff" font-size="15" font-family="system-ui"><text x="60" y="335" text-anchor="end">0</text><text x="60" y="255" text-anchor="end">700</text><text x="60" y="175" text-anchor="end">1,400</text><text x="60" y="95" text-anchor="end">2,100</text><text x="465" y="395" text-anchor="middle">Continuous operating days</text><text transform="translate(24 220) rotate(-90)" text-anchor="middle">Ideal Δv (m/s)</text><text x="90" y="355" text-anchor="middle">0</text><text x="265" y="355" text-anchor="middle">7</text><text x="440" y="355" text-anchor="middle">14</text><text x="615" y="355" text-anchor="middle">21</text><text x="840" y="355" text-anchor="middle">30</text></g><polyline points="{poly}" fill="none" stroke="#5bd4e8" stroke-width="5"/>{''.join(marks)}<text x="870" y="118" fill="#ffcf85" font-size="16" font-family="system-ui">30 d: ~1,586 m/s</text><text x="870" y="150" fill="#dce8ff" font-size="14" font-family="system-ui">~57.3 kg propellant</text><text x="90" y="423" fill="#a9bad7" font-size="13" font-family="system-ui">Calculated from NASA lab data; constant power/thrust/Isp; ideal rocket equation. No trajectory or flight result.</text></svg>'''
(HERE / "aeps-measured-mission-example.svg").write_text(svg, encoding="utf-8")
mobile_points = []
for day in range(31):
    dv = VE * math.log(M0 / (M0 - MDOT * day * 86400))
    mobile_points.append((55 + day * (310 / 30), 235 - dv * .09, day, dv))
mobile_poly = " ".join(f"{x:.1f},{y:.2f}" for x, y, _, _ in mobile_points)
mobile_marks = "".join(f'<circle cx="{x:.1f}" cy="{y:.2f}" r="4" fill="#ffbf69"/>' for x, y, day, _ in mobile_points if day in (0,10,20,30))
mobile_svg = f'''<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 395" role="img" aria-labelledby="title desc"><title id="title">Ideal velocity change in a hypothetical 1,000 kilogram spacecraft</title><desc id="desc">Calculated ideal velocity change from the measured NASA AEPS laboratory point: about 51 metres per second after one day and 1,586 metres per second after 30 continuous days. Assumes constant thrust and specific impulse. No trajectory or flight result.</desc><rect width="400" height="395" rx="18" fill="#0e1c35"/><text x="200" y="30" text-anchor="middle" fill="#fff" font-size="20" font-family="system-ui" font-weight="700">Ideal Δv · 1,000 kg example</text><g stroke="#7184a4" fill="#dce8ff" font-family="system-ui" font-size="20"><path d="M55 65V235H365" fill="none"/><path d="M55 155H365M55 75H365" opacity=".5"/><text x="48" y="241" text-anchor="end" stroke="none">0</text><text x="48" y="161" text-anchor="end" stroke="none">800</text><text x="48" y="81" text-anchor="end" stroke="none">1,600</text><text x="55" y="261" text-anchor="middle" stroke="none">0</text><text x="158.3" y="261" text-anchor="middle" stroke="none">10</text><text x="261.7" y="261" text-anchor="middle" stroke="none">20</text><text x="365" y="261" text-anchor="middle" stroke="none">30</text></g><text x="210" y="286" text-anchor="middle" fill="#dce8ff" font-size="20" font-family="system-ui">Continuous operating days</text><text transform="translate(18 154) rotate(-90)" text-anchor="middle" fill="#dce8ff" font-size="20" font-family="system-ui">Ideal Δv (m/s)</text><polyline points="{mobile_poly}" fill="none" stroke="#5bd4e8" stroke-width="4"/>{mobile_marks}<text x="22" y="321" fill="#ffcf85" font-size="20" font-family="system-ui" font-weight="700">1 day: ~51 m/s</text><text x="22" y="347" fill="#ffcf85" font-size="20" font-family="system-ui" font-weight="700">30 days: ~1,586 m/s</text><text x="22" y="373" fill="#c3d0e8" font-size="20" font-family="system-ui">No trajectory · no flight result</text></svg>'''
(HERE / "aeps-measured-mission-example-mobile.svg").write_text(mobile_svg, encoding="utf-8")
print("Rebuilt AEPS idealized mission CSV and SVG from NASA test row 8.")
