// products.jsx — 服務 / Services in the nav: the dropdown, plus the IP 千倍獲客漏斗 (IP 1000× Growth Funnel) page.
// The second service, 教育 AI 漏斗, lives in education.jsx.
// Built from the shared logic of the client proposals: amplify 1000× → the only limit is people → distil
// the expert into an AI persona → one all-in service from content to conversion → why 1000× → it equals a
// whole department at under 1/10 of the cost → an anonymous case → we run it, you approve → four phases.
// No client or partner names anywhere. Copy: t.products in i18n.jsx. Route: #/services/ip-growth-funnel (old #/products/ link still works).

const PRODUCT_ROUTES = { 'ip-growth-funnel': 'ipf', 'content-marketing-funnel': 'edu', 'education-ai-funnel': 'edu' };

// Nav dropdown: 服務 ▾ → service card.
function ProductsMenu({ t, onNav, route }) {
  const p = t.products;
  const [open, setOpen] = useState(false);
  const ref = useRef(null);
  useEffect(() => {
    const close = (e) => { if (ref.current && !ref.current.contains(e.target)) setOpen(false); };
    document.addEventListener('click', close);
    return () => document.removeEventListener('click', close);
  }, []);
  return (
    <div className="pm" ref={ref} onMouseEnter={() => setOpen(true)} onMouseLeave={() => setOpen(false)}>
      <button type="button" className={'pm-btn' + (Object.values(PRODUCT_ROUTES).includes(route) ? ' active' : '')} aria-expanded={open}
              onClick={() => setOpen((o) => !o)}>
        {p.menu} <span className="pm-caret" aria-hidden="true">▾</span>
      </button>
      {open && (
        <div className="pm-panel" role="menu">
          {p.items.map((it) => (
            <a key={it.slug} role="menuitem" className="pm-item" href={`#/services/${it.slug}`}
               onClick={(e) => { e.preventDefault(); setOpen(false); onNav(PRODUCT_ROUTES[it.slug]); }}>
              <span className="pm-ic"><i className="ic">{IC[it.i || 'bolt']}</i></span>
              <span className="pm-txt"><b>{it.name}{it.badge && <em>{it.badge}</em>}</b><small>{it.desc}</small></span>
              <Arrow size={12} />
            </a>
          ))}
        </div>
      )}
    </div>
  );
}

// One of you → a thousand: 1,000 dots light up outward from your clone in the middle.
function ThousandGrid({ label }) {
  const ref = useRef(null);
  const seen = useInView(ref, 0.2);
  const COLS = 40, ROWS = 25, cx = (COLS - 1) / 2, cy = (ROWS - 1) / 2;
  const dots = [];
  for (let r = 0; r < ROWS; r++) for (let c = 0; c < COLS; c++) {
    const d = Math.hypot((c - cx) / 1.6, r - cy);
    dots.push(<i key={r * COLS + c} style={{ animationDelay: `${(d * 0.11).toFixed(2)}s` }} />);
  }
  return (
    <div ref={ref} className={'tg' + (seen ? ' on' : '')} aria-hidden="true">
      <div className="tg-grid" style={{ gridTemplateColumns: `repeat(${COLS}, 1fr)` }}>{dots}</div>
      <div className="tg-core">
        <video src="assets/angles/ai-clone.mp4" poster="assets/angles/ai-clone.jpg" autoPlay muted loop playsInline preload="metadata" />
      </div>
      <div className="tg-count"><b>1 → <CountUp to={1000} run={seen} />×</b><span>{label}</span></div>
    </div>
  );
}

// The only limit is people: one lane where tasks queue, versus seven lanes where they flow.
function Bottleneck({ l }) {
  return (
    <div className="bn" aria-hidden="true">
      <div className="bn-box one">
        <div className="bn-head"><i className="ic">{IC.user}</i>{l.one}</div>
        {l.tasks.map((x, i) => (
          <div key={i} className="bn-lane">
            {i === 0 ? <span className="bn-chip slow">{x}</span> : <><span className="bn-chip wait">{x}</span><em>{l.waiting}</em></>}
          </div>
        ))}
      </div>
      <div className="bn-box ours">
        <div className="bn-head"><i className="ic">{IC.bolt}</i>{l.ours}</div>
        {l.tasks.map((x, i) => (
          <div key={i} className="bn-lane"><span className="bn-chip fast" style={{ animationDelay: `${-i * 0.37}s` }}>{x}</span></div>
        ))}
      </div>
    </div>
  );
}

// Distil: knowledge, voice and look flow into one AI persona; every sentence passes a red line.
function Distill({ d }) {
  const paths = ['M 150 60 C 260 60, 260 170, 360 170', 'M 150 170 C 250 170, 260 170, 360 170', 'M 150 280 C 260 280, 260 170, 360 170'];
  return (
    <div className="ds" aria-hidden="true">
      <svg viewBox="0 0 560 340" className="ds-svg">
        {paths.map((p, i) => (
          <g key={i}>
            <path d={p} className="ds-path" />
            {[0, 1, 2].map((k) => (
              <circle key={k} r="4" className="ds-dot">
                <animateMotion dur="2.4s" begin={`-${k * 0.8 + i * 0.25}s`} repeatCount="indefinite" path={p} />
              </circle>
            ))}
          </g>
        ))}
      </svg>
      <div className="ds-sources">
        {d.sources.map((s, i) => (
          <div key={i} className="ds-src"><i className="ic">{IC[s.i]}</i><div><b>{s.t}</b><small>{s.s}</small></div></div>
        ))}
      </div>
      <div className="ds-out">
        <div className="ds-face"><video src="assets/angles/ai-clone.mp4" poster="assets/angles/ai-clone.jpg" autoPlay muted loop playsInline preload="metadata" /></div>
        <b>{d.out}</b>
        <span className="ds-red"><i className="ic">{IC.check}</i>{d.redline}</span>
      </div>
    </div>
  );
}

// Seven services as one funnel: inputs pour in at the top, a light runs down to conversion.
function ServiceFunnel({ s }) {
  return (
    <div className="sf">
      <div className="sf-inputs">{s.inputs.map((x, i) => <span key={i} style={{ animationDelay: `${i * 0.5}s` }}>{x}<i className="ic">{IC.arrow}</i></span>)}</div>
      <ol className="sf-list">
        {s.list.map((it, i) => (
          <li key={i} style={{ '--w': `${100 - i * 7}%`, animationDelay: `${i * 0.45}s` }}>
            <span className="sf-n">{String(i + 1).padStart(2, '0')}</span>
            <span className="sf-ic"><i className="ic">{IC[it.i]}</i></span>
            <b>{it.n}</b>
            <small>{it.d}</small>
          </li>
        ))}
      </ol>
    </div>
  );
}

function PowerCards({ p }) {
  const ref = useRef(null);
  const seen = useInView(ref, 0.2);
  return (
    <div ref={ref} className="why-grid pw-grid">
      {p.cards.map((c, i) => (
        <div key={i} className="why-card">
          <div className="why-top"><i className="ic">{IC[c.i]}</i><span>{c.l}</span>
            <b>{c.x ? <><CountUp to={c.x} run={seen} />{c.suf}</> : <>0{c.suf}</>}</b></div>
          <div className="why-from">{c.from}</div>
          <div className="why-arrow"><i className="ic">{IC.arrow}</i></div>
          <div className="why-to">{c.to}</div>
        </div>
      ))}
    </div>
  );
}

// A whole department versus us: ten roles, and a cost bar that shrinks to under a tenth.
function DeptVsUs({ d }) {
  const ref = useRef(null);
  const seen = useInView(ref, 0.25);
  return (
    <div ref={ref} className={'dv' + (seen ? ' on' : '')}>
      <div className="dv-team">
        {d.roles.map((r, i) => (
          <span key={i} style={{ animationDelay: `${i * 0.08}s` }}><i className="ic">{IC.user}</i>{r}</span>
        ))}
      </div>
      <div className="dv-bars">
        <div className="dv-row"><small>{d.deptLabel}</small><div className="dv-bar"><span className="full" /></div><b>100%</b></div>
        <div className="dv-row ours"><small>{d.ourLabel}</small><div className="dv-bar"><span className="tenth" /></div><b>{d.ourVal}</b></div>
        <div className="dv-unit">{d.unit}</div>
      </div>
    </div>
  );
}

// An anonymous past case: bars that grow, plus three numbers. No name, no photos.
function CaseBars({ c }) {
  const ref = useRef(null);
  const seen = useInView(ref, 0.25);
  const max = Math.max(...c.bars.map((b) => b.v));
  return (
    <div ref={ref} className={'cb' + (seen ? ' on' : '')}>
      <div className="cb-tag">{c.tag}</div>
      <div className="cb-bars">
        {c.bars.map((b, i) => (
          <div key={i} className="cb-col">
            <b><CountUp to={b.v} run={seen} />{b.u}</b>
            <div className="cb-bar"><span style={{ height: `${(b.v / max) * 100}%`, transitionDelay: `${i * 0.25}s` }} /></div>
            <small>{b.l}</small>
          </div>
        ))}
      </div>
      <div className="cb-stats">{c.stats.map((s, i) => <div key={i}><b>{s.n}</b><small>{s.l}</small></div>)}</div>
      <div className="mod-note" style={{ textAlign: 'left' }}>{c.src}</div>
    </div>
  );
}

function Block({ eyebrow, title, body, children, tight }) {
  return (
    <section className={'section' + (tight ? ' section-tight' : '')}>
      <div className="wrap">
        <div className="section-head">
          <div className="eyebrow">{eyebrow}</div>
          <h2 className="h-1">{title}</h2>
          {body && <p className="lede">{body}</p>}
        </div>
        {children}
      </div>
    </section>
  );
}

function ProductIPFunnel({ t, onNav }) {
  const p = t.products.ipf;
  return (
    <div className="ipf-page">
      <section className="section ipf-hero">
        <div className="wrap ipf-hero-grid">
          <div>
            <div className="kicker" style={{ marginBottom: 28 }}><Dot /> {p.kicker}</div>
            <h1 className="h-display ipf-title">{p.title1}<br /><span style={{ color: 'var(--accent)' }}>{p.title2}</span></h1>
            <p className="lede" style={{ marginTop: 24 }}>{p.lede}</p>
            <div style={{ display: 'flex', gap: 12, flexWrap: 'wrap', marginTop: 28 }}>
              <button className="btn btn-accent" onClick={() => onNav('contact')}>{p.cta1} <Arrow /></button>
              <a className="btn btn-ghost" href="#ipf-services" onClick={(e) => { e.preventDefault(); scrollToId('ipf-services'); }}>{p.cta2} <Arrow /></a>
            </div>
          </div>
          <ThousandGrid label={p.heroNote} />
        </div>
      </section>

      <Block eyebrow={p.limit.eyebrow} title={p.limit.title} body={p.limit.body} tight><Bottleneck l={p.limit} /></Block>
      <Block eyebrow={p.distill.eyebrow} title={p.distill.title} body={p.distill.body} tight><Distill d={p.distill} /></Block>
      <div id="ipf-services"><Block eyebrow={p.services.eyebrow} title={p.services.title} tight><ServiceFunnel s={p.services} /></Block></div>
      <Block eyebrow={p.power.eyebrow} title={p.power.title} tight>
        <PowerCards p={p.power} />
        <p className="why-note">{p.power.note}</p>
      </Block>
      <Block eyebrow={p.dept.eyebrow} title={p.dept.title} body={p.dept.body} tight><DeptVsUs d={p.dept} /></Block>
      <Block eyebrow={p.caseB.eyebrow} title={p.caseB.title} tight><CaseBars c={p.caseB} /></Block>
      <Block eyebrow={p.split.eyebrow} title={p.split.title} tight>
        <div className="ipf-two">
          <div className="ipf-box"><div className="eyebrow">{p.split.youT}</div>
            <ul className="ipf-get">{p.split.you.map((x, i) => <li key={i}><i className="ic">{IC.user}</i>{x}</li>)}</ul></div>
          <div className="ipf-box ours"><div className="eyebrow">{p.split.usT}</div>
            <ul className="ipf-get">{p.split.us.map((x, i) => <li key={i}><i className="ic">{IC.check}</i>{x}</li>)}</ul></div>
        </div>
      </Block>
      <Block eyebrow={p.phases.eyebrow} title={p.phases.title} tight>
        <div className="process method-steps ipf-phases">
          {p.phases.list.map((s, i) => (
            <div key={i} className="step">
              <div className="step-num">/{s.n} · {s.w}</div>
              <div className="step-title">{s.t}</div>
              <div className="step-body">{s.b}</div>
            </div>
          ))}
        </div>
      </Block>
      <CTAStrip t={t} onNav={onNav} />
    </div>
  );
}

Object.assign(window, { ProductsMenu, ProductIPFunnel, PRODUCT_ROUTES });
