// Journey.jsx — a jornada de 1 ano (2º sem do 2º ano → 1º sem do 3º ano), timeline com progresso ligado ao scroll
function Journey() {
  const railRef = React.useRef(null);
  const [fill, setFill] = React.useState(0);

  React.useEffect(() => {
    const onScroll = () => {
      const el = railRef.current;
      if (!el) return;
      const r = el.getBoundingClientRect();
      const vh = window.innerHeight;
      // progresso: 0 quando o topo do rail entra na metade da tela, 1 quando o fim passa
      const total = r.height + vh * 0.2;
      const passed = vh * 0.6 - r.top;
      setFill(Math.max(0, Math.min(1, passed / total)));
    };
    onScroll();
    window.addEventListener('scroll', onScroll, { passive: true });
    return () => window.removeEventListener('scroll', onScroll);
  }, []);

  const milestones = [
    {
      letter: 'I', color: 'var(--aurora-blue-500)',
      when: '2º ano · 2º semestre', phase: 'Idea',
      title: 'A ideia sai do papel.',
      body: 'Repertório de IA, exploração de problemas reais e escolha do projeto. A fase fecha com um pitch de conceito defendido diante dos mentores.',
    },
    {
      letter: 'D', color: 'var(--aurora-coral-500)',
      when: 'Virada do ano', phase: 'Development',
      title: 'O MVP toma forma.',
      body: 'Ritmo de estúdio: construir com ferramentas de IA, errar rápido, iterar com feedback de usuários reais e mentoria de quem já constrói IA no mercado.',
    },
    {
      letter: 'L', color: 'var(--aurora-orange-500)',
      when: '3º ano · 1º semestre', phase: 'Launch',
      title: 'O piloto vai para o mundo.',
      body: 'Validação do piloto com usuários reais e fundamentos de negócio na prática: problema, mercado, modelo e métricas, a base do pitch para investidores.',
    },
    {
      letter: '★', color: 'var(--aurora-gold-500)',
      when: 'Encerramento', phase: 'Demo Day',
      title: 'O palco para quem decide.',
      body: 'Cada equipe apresenta produto, resultados e negócio para uma banca de investidores, universidades e comunidade escolar.',
    },
  ];

  return (
    <section id="jornada" data-screen-label="Jornada" style={{ padding: '160px 40px 140px', maxWidth: 1100, margin: '0 auto' }}>
      <Reveal style={{ marginBottom: 88, textAlign: 'center' }}>
        <div style={{
          fontFamily: 'var(--font-body)', fontWeight: 600, fontSize: 12,
          letterSpacing: '0.18em', textTransform: 'uppercase', color: 'var(--aurora-coral-500)',
          display: 'flex', alignItems: 'center', gap: 12, marginBottom: 24, justifyContent: 'center',
        }}>
          <span style={{ width: 32, height: 1, background: 'var(--aurora-coral-500)' }} />
          A jornada
          <span style={{ width: 32, height: 1, background: 'var(--aurora-coral-500)' }} />
        </div>
        <h2 style={{
          fontFamily: 'var(--font-display)', fontWeight: 600,
          fontSize: 'clamp(36px, 4.6vw, 68px)', lineHeight: 1.04,
          letterSpacing: '-0.025em', margin: 0, color: 'var(--aurora-navy-950)', textWrap: 'balance',
        }}>
          Um ano. Dois semestres. <em style={{ fontFamily: 'var(--font-accent)', fontWeight: 400 }}>Um lançamento.</em>
        </h2>
        <p style={{
          fontFamily: 'var(--font-body)', fontWeight: 300, fontSize: 19, lineHeight: 1.55,
          color: 'var(--aurora-ink-500)', maxWidth: 620, margin: '28px auto 0',
        }}>
          O programa acompanha o aluno do 2º semestre do 2º ano até o 1º semestre do 3º ano, o momento exato em que universidades e o mundo começam a olhar para ele.
        </p>
      </Reveal>

      {/* timeline */}
      <div ref={railRef} style={{ position: 'relative', paddingLeft: 0 }}>
        {/* rail de fundo */}
        <div aria-hidden style={{
          position: 'absolute', left: 27, top: 8, bottom: 8, width: 2,
          background: 'var(--color-divider)', borderRadius: 2,
        }} />
        {/* rail preenchido pelo scroll */}
        <div aria-hidden style={{
          position: 'absolute', left: 27, top: 8, width: 2,
          height: `calc(${(fill * 100).toFixed(2)}% - 16px)`,
          background: 'linear-gradient(180deg, var(--aurora-blue-500), var(--aurora-coral-500), var(--aurora-orange-500))',
          borderRadius: 2, transition: 'height 120ms linear',
        }} />

        <div style={{ display: 'flex', flexDirection: 'column', gap: 56 }}>
          {milestones.map((m, i) => {
            const reached = fill >= (i + 0.5) / milestones.length;
            return <JourneyRow key={i} m={m} index={i} reached={reached} />;
          })}
        </div>
      </div>
    </section>
  );
}

function JourneyRow({ m, index, reached }) {
  const [ref, seen] = useReveal();
  return (
    <div ref={ref} className={`card-reveal ${seen ? 'in' : ''}`} style={{
      display: 'grid', gridTemplateColumns: '56px 1fr', gap: 32, alignItems: 'start',
      animationDelay: seen ? `${index * 80}ms` : undefined, position: 'relative',
    }}>
      {/* nó da timeline */}
      <div style={{
        width: 56, height: 56, borderRadius: 999, position: 'relative', zIndex: 1,
        background: reached ? m.color : '#fff',
        border: `2px solid ${reached ? m.color : 'var(--color-border-strong)'}`,
        display: 'flex', alignItems: 'center', justifyContent: 'center',
        fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 22,
        color: reached ? '#fff' : 'var(--aurora-ink-400)',
        boxShadow: reached ? `0 0 0 8px ${index === 3 ? 'rgba(255,193,7,0.12)' : 'rgba(61,123,255,0.10)'}` : 'none',
        transition: 'all 480ms var(--ease-out)',
      }}>
        {m.letter}
      </div>

      <div style={{
        background: '#fff', border: '1px solid var(--color-border)', borderRadius: 20,
        padding: '32px 36px', boxShadow: 'var(--shadow-xs)',
      }}>
        <div style={{
          display: 'flex', gap: 16, alignItems: 'center', flexWrap: 'wrap', marginBottom: 14,
          fontFamily: 'var(--font-body)', fontSize: 12, fontWeight: 600,
          letterSpacing: '0.14em', textTransform: 'uppercase',
        }}>
          <span style={{ color: m.color }}>{m.phase}</span>
          <span style={{ width: 4, height: 4, borderRadius: 999, background: 'var(--aurora-ink-300)' }} />
          <span style={{ color: 'var(--aurora-ink-500)' }}>{m.when}</span>
        </div>
        <h3 style={{
          fontFamily: 'var(--font-display)', fontWeight: 600,
          fontSize: 'clamp(24px, 2.4vw, 34px)', lineHeight: 1.1,
          letterSpacing: '-0.02em', margin: 0, color: 'var(--aurora-navy-950)',
        }}>{m.title}</h3>
        <p style={{
          fontFamily: 'var(--font-body)', fontWeight: 300, fontSize: 17, lineHeight: 1.6,
          color: 'var(--aurora-ink-500)', margin: '14px 0 0', maxWidth: 640,
        }}>{m.body}</p>
      </div>
    </div>
  );
}

window.Journey = Journey;
