/* keplerlab.jsx — KeplerTutor: the Kepler-orbit bench. Built on window.BenchKit
   (harness) + window.BenchFields (shared field math), so this file is just the FIGURE.
   compute = window.BenchFields.kepler — the SAME function server/benches/kepler.js calls,
   so client and server fields are identical by construction. Follows the gaslaws template:
   sliders → BenchFields.fn(state) → report + meters. */
(function () {
  const { useState, useMemo, useEffect } = React;
  const K = window.BenchKit, C = K.C, SVG_BG = K.SVG_BG, fmt = K.fmt;
  const TaskStrip = K.TaskStrip, StatMeter = K.StatMeter;
  const compute = (s) => window.BenchFields.kepler(s);

  function Slider({ label, value, set, min, max, step, unit, color, onUp }) {
    return (
      <div style={{ display: "flex", alignItems: "center", gap: 10, marginTop: 9 }}>
        <span style={{ fontSize: 12, color: C.mute, width: 116 }}>{label}</span>
        <input type="range" min={min} max={max} step={step} value={value} onChange={(e) => set(parseFloat(e.target.value))} onPointerUp={onUp} style={{ flex: 1 }} />
        <span style={{ color: color || C.amber, width: 64, textAlign: "right" }}>{value}{unit ? ` ${unit}` : ""}</span>
      </div>
    );
  }

  function KeplerFig({ task, setTask, tasks, report, event, done }) {
    const [semiMajorAxis, setA] = useState(5);
    const [starMass, setM] = useState(1);
    const fld = useMemo(() => compute({ semiMajorAxis, starMass }), [semiMajorAxis, starMass]);
    useEffect(() => { report(fld); }, [semiMajorAxis, starMass]); // eslint-disable-line

    // orbit: star at center; orbit radius scales with a (0.2..30 AU → px); star size ∝ mass.
    const CX = 300, CY = 150;
    const rx = 36 + (semiMajorAxis / 30) * 230;
    const ry = rx * 0.62;
    const starR = 7 + starMass * 7;
    const t = tasks.find((x) => x.id === task) || tasks[0];
    const period = fld.period;

    return (
      <>
        <TaskStrip tasks={tasks} cur={task} setCur={setTask} done={done} goal={t && t.goal} />
        <svg viewBox="0 0 600 300" style={{ width: "100%", background: SVG_BG, border: `1px solid ${C.line}`, borderRadius: 8 }}>
          {/* orbit path */}
          <ellipse cx={CX} cy={CY} rx={rx} ry={ry} fill="none" stroke={C.faint} strokeWidth="1.5" />
          {/* star */}
          <circle cx={CX} cy={CY} r={starR} fill={C.gold} opacity="0.9" />
          {/* planet on the orbit (right vertex) */}
          <circle cx={CX + rx} cy={CY} r={6} fill={C.blue} />
          <line x1={CX} y1={CY} x2={CX + rx} y2={CY} stroke={C.teal} strokeWidth="1" strokeDasharray="3 3" />
          <text x={CX + rx / 2} y={CY - 6} textAnchor="middle" fill={C.mute} fontSize="11" fontFamily="monospace">a = {fmt(semiMajorAxis)} AU</text>
          <text x={CX} y={CY + starR + 16} textAnchor="middle" fill={C.mute} fontSize="11" fontFamily="monospace">M = {fmt(starMass)} M☉</text>
          <text x={CX} y={28} textAnchor="middle" fill={C.ink} fontSize="13" fontFamily="monospace">T = {fmt(period)} yr</text>
        </svg>
        <div style={{ marginTop: 10, padding: "8px 12px", borderRadius: 6, background: C.panel, border: `1px solid ${C.line}`, color: C.mute, fontSize: 12.5 }}>
          Kepler III · T² = a³ / M · T = {fmt(period)} yr. {fld.oneYearOrbit ? "✓ one Earth year." : "Widen a (↑T) or lighten the star (↑T)."}
        </div>
        <Slider label="semi-major axis a" value={semiMajorAxis} set={setA} min={0.2} max={30} step={0.1} unit="AU" color={C.blue} onUp={() => event("adjusted", `Set a = ${fmt(semiMajorAxis)} AU`, { response: `T ${fmt(fld.period)} yr` }, C.blue)} />
        <Slider label="star mass M" value={starMass} set={setM} min={0.1} max={3} step={0.05} unit="M☉" color={C.gold} onUp={() => event("adjusted", `Set M = ${fmt(starMass)} M☉`, { response: `T ${fmt(fld.period)} yr` }, C.gold)} />
        <div style={{ display: "grid", gridTemplateColumns: "repeat(4,1fr)", gap: 8, marginTop: 12 }}>
          <StatMeter label="period T" v={fld.period} d={2} unit="yr" color={C.teal} />
          <StatMeter label="semi-major a" v={fld.semiMajorAxis} d={2} unit="AU" color={C.blue} />
          <StatMeter label="star mass" v={fld.starMass} d={2} unit="M☉" color={C.gold} />
          <StatMeter label="T²" v={fld.periodSq} d={2} unit="yr²" color={C.amber} />
        </div>
      </>
    );
  }

  window.KeplerTutor = K.makeTutor(KeplerFig, { moduleLabel: "Kepler-orbit bench", benchId: "kepler" });
})();
