// Retro.jsx — orchestrates intro → voting → debrief for the sprint retro.
// Each phase is in its own file: RetroIntro / RetroVoting / RetroDebrief.

const { useState: useStateR, useMemo: useMemoR } = React;

function Retro({ sg, nickname, journey, onDotsChange, onExit, onPlanAnother, onRunAnotherSprint }) {
  const items = useMemoR(() => window.retroBuild(journey?.featureban || {}), [journey]);
  const [phase, setPhase] = useStateR("intro");
  const [votes, setVotes] = useStateR({});
  const [seed, setSeed] = useStateR(0);

  if (phase === "intro") {
    return (
      <window.RetroIntro
        items={items}
        totalVotes={window.RETRO_TOTAL_VOTES}
        sgGoal={sg?.goal}
        onStart={() => setPhase("voting")}
        onBack={onExit}
      />
    );
  }
  if (phase === "voting") {
    return (
      <window.RetroVoting
        key={seed}
        items={items}
        sgGoal={sg?.goal}
        onLock={(v) => {
          setVotes(v);
          const dots = Object.values(v).filter(Boolean).length;
          onDotsChange?.(dots);
          setPhase("done");
        }}
        onBack={() => setPhase("intro")}
      />
    );
  }
  return (
    <window.RetroDebrief
      items={items}
      votes={votes}
      nickname={nickname}
      journey={journey}
      onReplay={() => {
        setVotes({});
        setSeed((s) => s + 1);
        onDotsChange?.(0);
        setPhase("voting");
      }}
      onLeaderReview={undefined}
      onPlanAnother={onPlanAnother}
      onRunAnotherSprint={onRunAnotherSprint}
    />
  );
}

Object.assign(window, { Retro });
