/* criticalpointslab.jsx — CriticalPointsTutor: the critical points & 2nd-derivative-test bench.
   Built on window.BenchKit (harness) + window.BenchFields (shared field math), so this file is just
   the FIGURE. computeFields = window.BenchFields.criticalpoints — the SAME function
   server/benches/criticalpoints.js calls, so client and server fields are identical by construction.
   The learner picks a quadric surface f = a·x² + c·y² (bowl/dome/saddle) and drives a point (x,y)
   over its contour map; the partials fx, fy, the discriminant D = fxx·fyy and the classification at
   the lone critical point (the origin) update live. */
(function () {
  const { useState, useMemo, useEffect } = React;
  const K = window.BenchKit, C = K.C, fmt = K.fmt;
  const TaskStrip = K.TaskStrip, StatMeter = K.StatMeter, Scene3D = K.Scene3D;
  const compute = (s) => window.BenchFields.criticalpoints(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 }}><K.T>{label}</K.T></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>
    );
  }

  const SHAPES = ['bowl', 'dome', 'saddle'];
  const LABEL = { bowl: 'a·x²+c·y²  a=1 c=1', dome: 'a·x²+c·y²  a=-1 c=-1', saddle: 'a·x²+c·y²  a=1 c=-1' };

  function CriticalPointsFig({ task, setTask, tasks, report, event, done }) {
    const [shape, setShape] = useState('saddle');
    const [x, setX] = useState(0);
    const [y, setY] = useState(0);
    const [az, setAz] = useState(46);
    const [el, setEl] = useState(24);
    const fld = useMemo(() => compute({ shape, x, y }), [shape, x, y]);
    useEffect(() => { report(fld); }, [shape, x, y]); // eslint-disable-line

    // surface z = a·x² + c·y²; (a,c) from the shape (same as server)
    const map = { bowl: [1, 1], dome: [-1, -1], saddle: [1, -1] };
    const [a, cc] = map[shape] || map.saddle;
    const zAt = (X, Y) => a * X * X + cc * Y * Y;
    const pz = zAt(x, y);
    const ptColor = fld.atCritical ? C.teal : C.amber;

    // frame the surface over [-3,3]² plus the base plane, the point, and the origin
    const fit = [];
    for (const xx of [-3, 3]) for (const yy of [-3, 3]) { fit.push([xx, yy, zAt(xx, yy)]); fit.push([xx, yy, 0]); }
    fit.push([x, y, pz], [0, 0, 0]);

    const build = (screen) => {
      // base plane + axes (drawn first, behind the surface)
      let out = K.poly3d(screen, [[-3, -3, 0], [3, -3, 0], [3, 3, 0], [-3, 3, 0]], "rgba(108,182,255,0.05)", C.line, 0.7);
      out += K.seg3d(screen, [-3, 0, 0], [3.2, 0, 0], C.faint, 0.8) + K.text3d(screen, [3.5, 0, 0], "x", C.faint);
      out += K.seg3d(screen, [0, -3, 0], [0, 3.2, 0], C.faint, 0.8) + K.text3d(screen, [0, 3.5, 0], "y", C.faint);
      // the surface mesh z = a·x²+c·y², height-coloured + Lambert-shaded
      out += K.surfaceMesh3d(screen, zAt, -3, 3, -3, 3, { n: 24 });
      // the lone critical point at the origin — ∇f = 0
      out += K.dot3d(screen, [0, 0, 0], 5, "none", C.teal);
      out += K.text3d(screen, [0.25, 0.25, 0], "∇f = 0", C.teal);
      // gradient arrow on the base plane in direction (fx, fy)
      const g = fld.gradMag || Math.hypot(fld.fx, fld.fy) || 1;
      const gradMag = Math.hypot(fld.fx, fld.fy);
      if (gradMag > 0.05) { const L = Math.min(2.4, 0.5 + g * 0.3); out += K.vec3d(screen, [x, y, 0], [x + (fld.fx / g) * L, y + (fld.fy / g) * L, 0], C.crimson, 2.4); }
      // dashed vertical drop from the base point up to the surface
      out += K.seg3d(screen, [x, y, 0], [x, y, pz], C.faint, 1, "3 3");
      out += K.dot3d(screen, [x, y, 0], 2.5, C.faint);
      // the moved point on the surface
      out += K.dot3d(screen, [x, y, pz], 5, ptColor, C.ink);
      return out;
    };

    const cls = fld.atCritical ? (fld.isSaddle ? "saddle point" : fld.isMinimum ? "local minimum" : "local maximum") : "not a critical point";
    const clsColor = fld.atCritical ? (fld.isSaddle ? C.gold : C.teal) : C.mute;

    const t = tasks.find((q) => q.id === task) || tasks[0];

    return (
      <>
        <TaskStrip tasks={tasks} cur={task} setCur={setTask} done={done} goal={t && t.goal} />
        <div style={{ display: "flex", gap: 6, marginTop: 8 }}>
          {SHAPES.map((s) => (
            <button key={s} onClick={() => { setShape(s); event("adjusted", `Chose ${s} surface`, { response: `D ${fmt(compute({ shape: s, x, y }).discriminant)}` }, C.blue); }}
              style={{ flex: 1, padding: "6px 8px", fontSize: 12, borderRadius: 6, cursor: "pointer", border: `1px solid ${shape === s ? C.blue : C.line}`, background: shape === s ? C.panel : "transparent", color: shape === s ? C.blue : C.mute }}>
              <K.T>{s}</K.T>
            </button>
          ))}
        </div>
        <Scene3D az={az} el={el} setAz={setAz} setEl={setEl} fit={fit} width={600} height={320} build={build}
          onUp={() => event("rotated", `View az ${Math.round(az)}° · el ${Math.round(el)}°`, { response: `D ${fmt(fld.discriminant)}` }, C.blue)} />
        <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", marginTop: 8, fontSize: 11.5, color: C.faint, fontFamily: "monospace" }}>
          <span><K.T>drag to orbit</K.T> · D = {fmt(fld.discriminant)} · <span style={{ color: clsColor }}>{cls}</span></span>
          <span>f = {LABEL[shape]}</span>
        </div>
        <div style={{ marginTop: 10, padding: "8px 12px", borderRadius: 6, background: C.panel, border: `1px solid ${fld.atCritical ? C.teal : C.line}`, color: fld.atCritical ? C.teal : C.mute, fontSize: 12.5 }}>
          {fld.atCritical
            ? <>⚑ <K.T>At the critical point (origin).</K.T> {fld.isSaddle ? <K.T>D &lt; 0 — a saddle.</K.T> : fld.isMinimum ? <K.T>D &gt; 0, fxx &gt; 0 — a local minimum.</K.T> : <K.T>D &gt; 0, fxx &lt; 0 — a local maximum.</K.T>}</>
            : <K.T>Both partials must vanish to be a critical point — drive the point onto the origin.</K.T>}
        </div>
        <Slider label="point x" value={x} set={setX} min={-3} max={3} step={0.25} unit="" color={C.teal} onUp={() => event("adjusted", `Set x = ${fmt(x)}`, { response: `fx ${fmt(fld.fx)}` }, C.teal)} />
        <Slider label="point y" value={y} set={setY} min={-3} max={3} step={0.25} unit="" color={C.gold} onUp={() => event("adjusted", `Set y = ${fmt(y)}`, { response: `fy ${fmt(fld.fy)}` }, C.gold)} />
        <div style={{ display: "grid", gridTemplateColumns: "repeat(4,1fr)", gap: 8, marginTop: 12 }}>
          <StatMeter label="∂f/∂x" v={fld.fx} d={2} color={C.blue} />
          <StatMeter label="∂f/∂y" v={fld.fy} d={2} color={C.gold} />
          <StatMeter label="D" v={fld.discriminant} d={2} color={C.teal} />
          <StatMeter label="at critical" v={fld.atCritical ? 1 : 0} d={0} color={fld.atCritical ? C.teal : C.mute} />
        </div>
      </>
    );
  }

  window.CriticalPointsTutor = K.makeTutor(CriticalPointsFig, { moduleLabel: "Critical Points bench", benchId: "criticalpoints" });
})();
