/* global React, Kicker, LimeCheck, TopoLines, RecruitButton */

// Who it's for — qualification checklist; bold uses parchment.
function WhoFor({ variant }) {
  const isBold = variant === 'bold';
  const fits = [
    '3–5 years selling Medicare and still excited about the business',
    'A client book of 100+ active households',
    'Willing to show up weekly for 10 weeks and do the reps',
    'Curious about new systems — not just looking for a shortcut',
  ];
  const nope = [
    'Brand new to insurance or still building your first 50 clients',
    'Looking for lead-gen only, no product skill-up',
    'Not licensed to sell life/annuities in your state',
  ];
  return (
    <section id="who" style={{
      padding: '96px 24px',
      background: '#071326',
      color: '#fff', position: 'relative', overflow: 'hidden',
    }}>
      <TopoLines opacity={0.10} color="#BADA55"/>
      <div style={{ maxWidth: 1200, margin: '0 auto', position: 'relative' }}>
        <Kicker color="#BADA55">
          {isBold ? '⛰ Who we rope in' : "Who this is for"}
        </Kicker>
        <h2 style={{
          fontFamily: '"Lato", sans-serif',
          fontSize: 'clamp(30px, 3vw, 44px)', fontWeight: 900, color: '#fff',
          margin: '0 0 48px', lineHeight: 1.12, maxWidth: '22ch', letterSpacing: '-0.01em',
        }}>
          {isBold ? <>Only 10 rope into this expedition. <span style={{ color: '#BADA55' }}>Are you one of them?</span></>
                  : <>This is built for <span style={{ color: '#1495B9' }}>working Medicare agents</span> ready for the next climb.</>}
        </h2>
        <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 40 }}>
          <div style={{ background: 'rgba(255,255,255,0.04)', border: '1px solid rgba(186,218,85,0.25)', borderRadius: 20, padding: 32 }}>
            <div style={{ fontSize: 12, fontWeight: 700, letterSpacing: '0.16em', textTransform: 'uppercase', color: '#BADA55', marginBottom: 18 }}>You belong in the cohort if</div>
            <ul style={{ listStyle: 'none', padding: 0, margin: 0 }}>
              {fits.map(f => <LimeCheck key={f} onDark>{f}</LimeCheck>)}
            </ul>
          </div>
          <div style={{ background: 'rgba(255,255,255,0.02)', border: '1px solid rgba(255,255,255,0.12)', borderRadius: 20, padding: 32 }}>
            <div style={{ fontSize: 12, fontWeight: 700, letterSpacing: '0.16em', textTransform: 'uppercase', color: 'rgba(255,255,255,0.55)', marginBottom: 18 }}>Not the right trail yet if</div>
            <ul style={{ listStyle: 'none', padding: 0, margin: 0 }}>
              {nope.map(f => (
                <li key={f} style={{ position: 'relative', paddingLeft: 36, marginBottom: 14, fontSize: 16, color: 'rgba(255,255,255,0.7)', lineHeight: 1.55 }}>
                  <span style={{ position: 'absolute', left: 0, top: 3, width: 22, height: 22, borderRadius: '50%', background: 'rgba(255,255,255,0.08)', border: '1px solid rgba(255,255,255,0.25)' }}/>
                  <span style={{ position: 'absolute', left: 7, top: 10, width: 8, height: 2, background: 'rgba(255,255,255,0.5)' }}/>
                  {f}
                </li>
              ))}
            </ul>
          </div>
        </div>
      </div>
    </section>
  );
}

// OUTCOMES
function Outcomes({ variant }) {
  const isBold = variant === 'bold';
  const items = [
    { ic: '📜', k: 'The language', v: 'Natural cross-sell scripts that move the conversation from health to wealth without breaking trust.' },
    { ic: '🧭', k: 'The products', v: 'Working fluency in MYGAs and FIAs — the two annuities your book actually needs first.' },
    { ic: '🪢', k: 'The process',  v: 'A repeatable workflow from first mention → quote → application → funded case.' },
    { ic: '⛺', k: 'The crew',    v: 'A back-office and cohort you can text when you get stuck in the middle of a client meeting.' },
  ];
  return (
    <section style={{
      padding: '96px 24px',
      background: 'linear-gradient(180deg, #FAF6E8 0%, #F5EFDC 100%)',
      position: 'relative', overflow: 'hidden',
    }}>
      <div style={{ maxWidth: 1200, margin: '0 auto' }}>
        <Kicker color="#3D2A15">{isBold ? '✦ What you carry home' : 'Outcomes'}</Kicker>
        <h2 style={{
          fontFamily: '"Lato", sans-serif',
          fontSize: 'clamp(30px, 3vw, 44px)', fontWeight: 900,
          color: '#0F2341', margin: '0 0 48px', lineHeight: 1.12, maxWidth: '22ch', letterSpacing: '-0.01em',
        }}>
          {isBold ? <>You'll leave basecamp with <span style={{ color: '#1495B9' }}>four things.</span></>
                  : <>By week 10, you'll have four things you don't have now.</>}
        </h2>
        <div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(240px, 1fr))', gap: 24 }}>
          {items.map(it => (
            <div key={it.k} style={{
              background: 'rgba(255,255,255,0.55)',
              border: '1px solid rgba(61,42,21,0.18)',
              borderRadius: 16, padding: 28,
            }}>
              <div style={{ fontSize: 28, marginBottom: 10 }}>{it.ic}</div>
              <div style={{ fontSize: 12, fontWeight: 700, letterSpacing: '0.14em', textTransform: 'uppercase', color: '#1495B9', marginBottom: 8 }}>{it.k}</div>
              <p style={{ fontSize: 15, color: '#212529', lineHeight: 1.55, margin: 0 }}>{it.v}</p>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

// Count-up animation for a single stat. Parses "$2B+", "3,400+", "48%", "93%"
// into (prefix, number, suffix) and animates the number once in view.
function AnimatedStat({ value }) {
  const ref = React.useRef(null);
  const [started, setStarted] = React.useState(false);
  const [display, setDisplay] = React.useState(value);

  // Parse — "$2B+" → prefix "$", num 2, suffix "B+", precision 0
  const parsed = React.useMemo(() => {
    const m = String(value).match(/^([^\d.]*)([\d,.]+)(.*)$/);
    if (!m) return null;
    const prefix = m[1];
    const numStr = m[2].replace(/,/g, '');
    const suffix = m[3];
    const target = parseFloat(numStr);
    const hasComma = m[2].includes(',');
    const decimals = (numStr.split('.')[1] || '').length;
    return { prefix, target, suffix, hasComma, decimals };
  }, [value]);

  React.useEffect(() => {
    if (!ref.current || !parsed) return;
    const io = new IntersectionObserver(entries => {
      entries.forEach(e => { if (e.isIntersecting) setStarted(true); });
    }, { threshold: 0.35 });
    io.observe(ref.current);
    return () => io.disconnect();
  }, [parsed]);

  React.useEffect(() => {
    if (!started || !parsed) return;
    const { prefix, target, suffix, hasComma, decimals } = parsed;
    const duration = 1600;
    const startT = performance.now();
    let raf;
    const tick = (now) => {
      const t = Math.min(1, (now - startT) / duration);
      // easeOutExpo
      const eased = t === 1 ? 1 : 1 - Math.pow(2, -10 * t);
      const cur = target * eased;
      let s = decimals > 0 ? cur.toFixed(decimals) : String(Math.round(cur));
      if (hasComma) s = Number(s).toLocaleString('en-US');
      setDisplay(prefix + s + suffix);
      if (t < 1) raf = requestAnimationFrame(tick);
    };
    // init at 0
    setDisplay(prefix + (decimals > 0 ? (0).toFixed(decimals) : '0') + suffix);
    raf = requestAnimationFrame(tick);
    return () => cancelAnimationFrame(raf);
  }, [started, parsed]);

  return (
    <div ref={ref} style={{
      fontFamily: '"Lato", sans-serif', fontWeight: 900,
      fontSize: 'clamp(40px, 4vw, 58px)', color: '#BADA55',
      lineHeight: 1, letterSpacing: '-0.02em',
      fontVariantNumeric: 'tabular-nums',
    }}>{display}</div>
  );
}

// PROOF — the Elevate numbers
function Proof({ variant }) {
  const isBold = variant === 'bold';
  const stats = [
    { n: '$2B+', l: 'Annuity premium written through our trained agents since 2017' },
    { n: '3,400+', l: 'Agents trained across BaseCamp and Elevate cohorts' },
    { n: '48%', l: 'Average Y1 revenue lift after cohort completion' },
    { n: '93%', l: 'Graduation rate across BaseCamp classes' },
  ];
  return (
    <section id="proof" style={{
      padding: '96px 24px',
      background: 'linear-gradient(180deg, #0F2341 0%, #071326 100%)',
      color: '#fff', position: 'relative', overflow: 'hidden',
    }}>
      <TopoLines opacity={0.08} color="#1495B9"/>
      <div style={{ maxWidth: 1200, margin: '0 auto', position: 'relative' }}>
        <Kicker color="#BADA55">{isBold ? '✦ The tally at the summit' : 'The track record'}</Kicker>
        <h2 style={{
          fontFamily: '"Lato", sans-serif',
          fontSize: 'clamp(30px, 3vw, 44px)', fontWeight: 900, color: '#fff',
          margin: '0 0 48px', lineHeight: 1.12, maxWidth: '22ch', letterSpacing: '-0.01em',
        }}>
          {isBold ? <>We've been up this mountain. <span style={{ color: '#BADA55' }}>Many times.</span></>
                  : <>Our training programs have been a cornerstone for our agents.</>}
        </h2>
        <div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(200px, 1fr))', gap: 0, border: '1px solid rgba(255,255,255,0.14)', borderRadius: 20 }}>
          {stats.map((s, i) => (
            <div key={s.n} style={{ padding: '36px 28px', borderRight: i < stats.length - 1 ? '1px solid rgba(255,255,255,0.1)' : 'none' }}>
              <AnimatedStat value={s.n}/>
              <div style={{ fontSize: 13.5, color: 'rgba(255,255,255,0.75)', marginTop: 10, lineHeight: 1.45 }}>{s.l}</div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

window.RecruitWho = WhoFor;
window.RecruitOutcomes = Outcomes;
window.RecruitProof = Proof;
