// workflow_v2.jsx — cinematic editorial version of the six-stage workflow.

const STAGES_V2 = [
  { num: '01', title: 'Build',     desc: 'Branded templates, practice questions, and guardrails on time.',     Icon: IconV2Clipboard },
  { num: '02', title: 'Score',     desc: 'Weighted rubrics with scoring anchors calibrated to the role.',      Icon: IconV2Scale },
  { num: '03', title: 'Invite',    desc: 'Auto-triggered the moment a candidate hits the workflow stage.',     Icon: IconV2Send },
  { num: '04', title: 'AI Assist', desc: 'Transcription and per-criterion scoring while the session is live.', Icon: IconV2Bot },
  { num: '05', title: 'Review',    desc: 'Calibrate human judgement against AI — side by side.',               Icon: IconV2Review },
  { num: '06', title: 'Report',    desc: 'Completion rates, time-to-hire, dashboards your leaders will read.',  Icon: IconV2Bars },
];

// ── Background: aurora wash over dark canvas ───────────────────────────
function V2Background() {
  const t = useTime();
  // slow drift
  const drift1 = Math.sin(t * 0.25) * 40;
  const drift2 = Math.cos(t * 0.18) * 60;
  return (
    <>
      {/* base gradient */}
      <div style={{
        position: 'absolute', inset: 0,
        background: 'radial-gradient(ellipse at 20% 10%, #0e1530 0%, #070712 60%)',
      }}/>
      {/* aurora blob 1 — teal */}
      <div style={{
        position: 'absolute',
        left: `${20 + drift1 * 0.2}%`, top: `${-10 + drift2 * 0.1}%`,
        width: 1200, height: 900,
        background: 'radial-gradient(ellipse at center, rgba(94,234,212,0.18) 0%, rgba(94,234,212,0) 55%)',
        filter: 'blur(20px)',
        pointerEvents: 'none',
      }}/>
      {/* aurora blob 2 — lilac */}
      <div style={{
        position: 'absolute',
        right: `${-10 + drift2 * 0.15}%`, bottom: `${-15 + drift1 * 0.1}%`,
        width: 1100, height: 900,
        background: 'radial-gradient(ellipse at center, rgba(167,139,250,0.15) 0%, rgba(167,139,250,0) 60%)',
        filter: 'blur(20px)',
        pointerEvents: 'none',
      }}/>
      {/* grain */}
      <div style={{
        position: 'absolute', inset: 0,
        backgroundImage: 'radial-gradient(circle at 1px 1px, rgba(255,255,255,0.025) 1px, transparent 0)',
        backgroundSize: '3px 3px',
        mixBlendMode: 'overlay',
        opacity: 0.6,
        pointerEvents: 'none',
      }}/>
    </>
  );
}

// ── Eyebrow + Headline ────────────────────────────────────────────────
function V2Headline() {
  const t = useTime();

  // Eyebrow reveals with letter-spacing expand
  const eyebrowT = Easing.easeOutCubic(clamp((t - 0.1) / 0.7, 0, 1));

  // Headline words reveal in sequence, each with a mask wipe.
  // Two-line beat: "Six stages." / "One confident hire."
  const words = [
    { text: 'Six',       breakAfter: false },
    { text: 'stages.',   breakAfter: true  },
    { text: 'One',       breakAfter: false },
    { text: 'confident', breakAfter: false, italic: true },
    { text: 'hire.',     breakAfter: false },
  ];
  const wordStart = 0.5;
  const wordStep = 0.18;

  // Support line fades in after headline
  const supportT = Easing.easeOutCubic(clamp((t - 1.8) / 0.9, 0, 1));

  // Accent rule underline
  const ruleT = Easing.easeOutCubic(clamp((t - 2.2) / 0.6, 0, 1));

  return (
    <div style={{ position: 'absolute', left: 140, top: 160, width: 1640 }}>
      {/* eyebrow with thin rule */}
      <div style={{
        display: 'flex', alignItems: 'center', gap: 18,
        opacity: eyebrowT,
        transform: `translateY(${(1 - eyebrowT) * 10}px)`,
      }}>
        <div style={{
          width: 56 * eyebrowT, height: 1,
          background: V2_TEAL,
        }}/>
        <div style={{
          fontFamily: 'Gilroy, sans-serif',
          fontSize: 15, fontWeight: 500,
          color: V2_TEAL,
          letterSpacing: `${0.32 + (1 - eyebrowT) * 0.3}em`,
          textTransform: 'uppercase',
        }}>
          How it works
        </div>
      </div>

      {/* Headline — serif display with mask wipe per word */}
      <div style={{
        marginTop: 28,
        fontFamily: 'Gilroy, sans-serif',
        fontSize: 132, fontWeight: 700,
        color: V2_INK,
        letterSpacing: '-0.035em',
        lineHeight: 1.05,
        whiteSpace: 'normal',
      }}>
        {words.map((w, i) => {
          const localT = Easing.easeOutCubic(clamp((t - (wordStart + i * wordStep)) / 0.6, 0, 1));
          const italic = w.italic;
          return (
            <React.Fragment key={i}>
              <span style={{
                display: 'inline-block',
                verticalAlign: 'bottom',
                clipPath: localT >= 1 ? 'none' : `inset(-0.3em -0.3em ${(1 - localT) * 100 - 20}% -0.3em)`,
                paddingBottom: '0.18em',
                marginRight: i < words.length - 1 && !w.breakAfter ? '0.32em' : 0,
              }}>
                <span style={{
                  display: 'inline-block',
                  transform: `translateY(${(1 - localT) * 100}%)`,
                  fontStyle: italic ? 'italic' : 'normal',
                  color: italic ? V2_TEAL : V2_INK,
                }}>
                  {w.text}
                </span>
              </span>
              {w.breakAfter && <div style={{ width: '100%', height: 0 }}/>}
            </React.Fragment>
          );
        })}
      </div>

      {/* supporting line */}
      <div style={{
        marginTop: 36,
        display: 'flex',
        alignItems: 'baseline',
        gap: 32,
        opacity: supportT,
        transform: `translateY(${(1 - supportT) * 12}px)`,
      }}>
        <div style={{
          width: 60 * ruleT, height: 1,
          background: 'rgba(231,237,243,0.3)',
        }}/>
        <div style={{
          fontFamily: 'Gilroy, sans-serif',
          fontSize: 22, fontWeight: 300,
          color: 'rgba(231,237,243,0.65)',
          letterSpacing: '-0.005em',
          maxWidth: 680,
          lineHeight: 1.4,
        }}>
          From the first prompt to the final dashboard — a workflow that sounds like your brand and scores like your best interviewer.
        </div>
      </div>
    </div>
  );
}

// ── The rail — a single line across, with a traveling light pulse ──────
function V2Rail({ y, startX, endX }) {
  const t = useTime();
  const RAIL_START = 3.0; // rail draws after headline
  const drawT = Easing.easeInOutCubic(clamp((t - RAIL_START) / 1.2, 0, 1));
  const width = (endX - startX) * drawT;

  // Pulse after rail drawn — travels on loop
  const PULSE_START = RAIL_START + 1.2;
  const pulsePhase = t < PULSE_START ? -1 : ((t - PULSE_START) % 4) / 4;
  const pulseX = startX + (endX - startX) * pulsePhase;
  const pulseActive = t >= PULSE_START;

  return (
    <>
      {/* Rail line */}
      <div style={{
        position: 'absolute',
        left: startX, top: y,
        width, height: 1,
        background: `linear-gradient(90deg, rgba(94,234,212,0) 0%, rgba(94,234,212,0.35) 12%, rgba(94,234,212,0.5) 50%, rgba(167,139,250,0.4) 88%, rgba(167,139,250,0) 100%)`,
      }}/>
      {/* Tick marks behind rail at each stage position */}
      {pulseActive && (
        <>
          {/* Traveling pulse — glow orb */}
          <div style={{
            position: 'absolute',
            left: pulseX - 20, top: y - 20,
            width: 40, height: 40,
            borderRadius: '50%',
            background: 'radial-gradient(circle, rgba(94,234,212,0.9) 0%, rgba(94,234,212,0) 70%)',
            filter: 'blur(2px)',
            pointerEvents: 'none',
          }}/>
          <div style={{
            position: 'absolute',
            left: pulseX - 3, top: y - 3,
            width: 6, height: 6,
            borderRadius: '50%',
            background: '#B8FBF0',
            boxShadow: '0 0 12px rgba(94,234,212,0.9)',
            pointerEvents: 'none',
          }}/>
        </>
      )}
    </>
  );
}

// ── Stage ──────────────────────────────────────────────────────────────
function V2Stage({ stage, index, cx, railY, activeIndex }) {
  const t = useTime();
  const RAIL_START = 3.0;
  const start = RAIL_START + 1.2 + index * 0.38; // after rail completes

  // Node (circle on rail)
  const nodeT = Easing.easeOutBack(clamp((t - start) / 0.55, 0, 1));

  // Icon draw progress (pen plot)
  const iconProgress = clamp((t - start - 0.2) / 0.9, 0, 1);

  // Number counts up
  const numT = clamp((t - start - 0.15) / 0.7, 0, 1);
  const numValue = Math.floor(interpolate([0, 1], [0, parseInt(stage.num, 10)], Easing.easeOutCubic)(numT));
  const numStr = String(numValue).padStart(2, '0');

  // Title mask wipe
  const titleT = Easing.easeOutCubic(clamp((t - start - 0.3) / 0.55, 0, 1));

  // Description line stagger
  const descT = Easing.easeOutCubic(clamp((t - start - 0.45) / 0.7, 0, 1));

  // Active spotlight (looped focus)
  const ALL_IN = RAIL_START + 1.2 + STAGES_V2.length * 0.38 + 0.6;
  const isActive = activeIndex === index;
  const activeGlow = isActive ? 1 : 0;

  const NODE_SIZE = 88;
  const topY = railY - NODE_SIZE / 2; // circle centered on rail

  const Icon = stage.Icon;

  // Active lift
  const liftY = isActive ? -8 : 0;

  return (
    <div style={{
      position: 'absolute',
      left: cx - 140, top: topY,
      width: 280,
      display: 'flex', flexDirection: 'column', alignItems: 'center',
      transform: `translateY(${liftY}px)`,
      transition: 'transform 500ms cubic-bezier(0.2, 0.8, 0.2, 1)',
    }}>
      {/* Halo — active spotlight */}
      <div style={{
        position: 'absolute',
        left: 140 - 110, top: NODE_SIZE/2 - 110,
        width: 220, height: 220,
        borderRadius: '50%',
        background: 'radial-gradient(circle, rgba(94,234,212,0.25) 0%, rgba(94,234,212,0) 60%)',
        opacity: activeGlow,
        transition: 'opacity 400ms ease-out',
        pointerEvents: 'none',
      }}/>

      {/* Node: circle on the rail */}
      <div style={{
        width: NODE_SIZE, height: NODE_SIZE,
        borderRadius: '50%',
        position: 'relative',
        opacity: nodeT,
        transform: `scale(${nodeT})`,
      }}>
        {/* outer ring */}
        <div style={{
          position: 'absolute', inset: 0,
          borderRadius: '50%',
          border: `1px solid rgba(94,234,212,${isActive ? 0.8 : 0.35})`,
          transition: 'border-color 400ms',
        }}/>
        {/* inner fill */}
        <div style={{
          position: 'absolute', inset: 6,
          borderRadius: '50%',
          background: isActive
            ? 'radial-gradient(circle at 30% 25%, rgba(94,234,212,0.25), rgba(14,26,48,0.95) 70%)'
            : 'radial-gradient(circle at 30% 25%, rgba(94,234,212,0.08), rgba(14,26,48,0.95) 70%)',
          boxShadow: isActive ? '0 0 32px rgba(94,234,212,0.35), inset 0 0 24px rgba(94,234,212,0.15)' : 'inset 0 0 18px rgba(0,0,0,0.4)',
          transition: 'all 400ms',
        }}/>
        {/* icon */}
        <div style={{
          position: 'absolute', inset: 0,
          display: 'flex', alignItems: 'center', justifyContent: 'center',
        }}>
          <div style={{ transform: `scale(${0.9})` }}>
            <Icon progress={iconProgress} />
          </div>
        </div>
      </div>

      {/* Number — serif, large */}
      <div style={{
        marginTop: 32,
        fontFamily: 'Gilroy, sans-serif',
        fontSize: 56, fontWeight: 700,
        color: isActive ? V2_TEAL : 'rgba(94,234,212,0.55)',
        letterSpacing: '-0.02em',
        lineHeight: 1,
        opacity: numT,
        fontVariantNumeric: 'tabular-nums',
        transition: 'color 400ms',
      }}>
        {numStr}
      </div>

      {/* Divider */}
      <div style={{
        marginTop: 14,
        width: 24 * titleT, height: 1,
        background: 'rgba(231,237,243,0.25)',
      }}/>

      {/* Title with mask wipe */}
      <div style={{
        marginTop: 14,
        overflow: 'hidden',
        height: 32,
      }}>
        <div style={{
          fontFamily: 'Gilroy, sans-serif',
          fontSize: 24, fontWeight: 600,
          color: V2_INK,
          letterSpacing: '-0.01em',
          whiteSpace: 'nowrap',
          transform: `translateY(${(1 - titleT) * 100}%)`,
        }}>
          {stage.title}
        </div>
      </div>

      {/* Description */}
      <div style={{
        marginTop: 14,
        fontFamily: 'Gilroy, sans-serif',
        fontSize: 14, fontWeight: 400,
        color: 'rgba(231,237,243,0.6)',
        lineHeight: 1.5,
        textAlign: 'center',
        maxWidth: 220,
        opacity: descT,
        transform: `translateY(${(1 - descT) * 8}px)`,
      }}>
        {stage.desc}
      </div>
    </div>
  );
}

// ── Footer meta (just a timestamp + brand mark for polish) ──────────────
function V2FooterMeta() {
  const t = useTime();
  const footerT = Easing.easeOutCubic(clamp((t - 7) / 1.2, 0, 1));

  return (
    <>
      {/* bottom-left mark */}
      <div style={{
        position: 'absolute', left: 140, bottom: 50,
        display: 'flex', alignItems: 'center', gap: 20,
        opacity: footerT,
        transform: `translateY(${(1 - footerT) * 12}px)`,
      }}>
        <img
          src="assets/TTP-logo-landscape-dark.svg"
          alt="The Talent People"
          style={{
            height: 44,
            width: 'auto',
            display: 'block',
            filter: 'invert(1) brightness(1.15)',
          }}
        />
        <div style={{
          width: 1, height: 20,
          background: 'rgba(231,237,243,0.2)',
        }}/>
        <div style={{
          fontFamily: 'Gilroy, sans-serif',
          fontSize: 12, fontWeight: 500,
          color: 'rgba(231,237,243,0.55)',
          letterSpacing: '0.14em',
          textTransform: 'uppercase',
        }}>
          TalentScreen / Workflow
        </div>
      </div>

      {/* bottom-right stage indicator */}
      <div style={{
        position: 'absolute', right: 140, bottom: 50,
        display: 'flex', alignItems: 'center', gap: 10,
        opacity: footerT,
        transform: `translateY(${(1 - footerT) * 12}px)`,
        fontFamily: 'Gilroy, sans-serif',
        fontSize: 12,
        color: 'rgba(231,237,243,0.45)',
        letterSpacing: '0.12em',
        textTransform: 'uppercase',
      }}>
        <span>06 stages</span>
        <div style={{ width: 1, height: 10, background: 'rgba(231,237,243,0.2)' }}/>
        <span>loop 00:14</span>
      </div>
    </>
  );
}

// ── Scene ─────────────────────────────────────────────────────────────
function WorkflowSceneV2() {
  const t = useTime();

  // Layout: rail spans across, 6 nodes evenly distributed
  const RAIL_Y = 680;                   // vertical position of rail
  const LEFT_PAD = 220;
  const RIGHT_PAD = 220;
  const startX = LEFT_PAD;
  const endX = 1920 - RIGHT_PAD;
  const trackW = endX - startX;
  const stepX = trackW / (STAGES_V2.length - 1);

  // Active spotlight loop
  const ALL_IN = 3.0 + 1.2 + STAGES_V2.length * 0.38 + 0.6;
  const ACTIVE_HOLD = 0.9; // each stage highlighted
  const activePhase = t < ALL_IN ? -1 : (t - ALL_IN) / ACTIVE_HOLD;
  const activeIndex = activePhase >= 0 ? Math.floor(activePhase) % STAGES_V2.length : -1;

  return (
    <>
      <V2Background />
      <V2Headline />
      <V2Rail y={RAIL_Y} startX={startX} endX={endX} />
      {STAGES_V2.map((stage, i) => (
        <V2Stage
          key={i}
          stage={stage}
          index={i}
          cx={startX + i * stepX}
          railY={RAIL_Y}
          activeIndex={activeIndex}
        />
      ))}
      <V2FooterMeta />
    </>
  );
}

Object.assign(window, { WorkflowSceneV2 });
