/* global React, useCountUp */
const { useEffect: useEffectM, useRef: useRefM, useState: useStateM } = React;

// ============ Marquee ticker ============
function Marquee() {
  // English-only marquee with airport codes
  const items = [
    { code: 'SHA', en: 'Shanghai → Rotterdam' },
    { code: 'SZX', en: 'Shenzhen → Los Angeles' },
    { code: 'NGB', en: 'Ningbo → Hamburg' },
    { code: 'CAN', en: 'Guangzhou → Dubai' },
    { code: 'TAO', en: 'Qingdao → New York' },
    { code: 'TSN', en: 'Tianjin → Antwerp' },
    { code: 'XMN', en: 'Xiamen → Singapore' },
    { code: 'DLC', en: 'Dalian → Felixstowe' },
  ];
  const all = [...items, ...items, ...items];
  return (
    <div id="routes" style={{
      background: 'var(--ink)',
      borderTop: '1px solid rgba(212, 168, 87, 0.2)',
      borderBottom: '1px solid rgba(212, 168, 87, 0.2)',
      overflow: 'hidden',
      padding: '20px 0',
      color: 'var(--paper)',
    }}>
      <div style={{
        display: 'flex',
        animation: 'marquee 50s linear infinite',
        whiteSpace: 'nowrap',
        gap: 64,
        width: 'max-content',
      }}>
        {all.map((it, i) => (
          <span key={i} style={{ display: 'inline-flex', alignItems: 'center', gap: 14, fontSize: 13, letterSpacing: '0.08em' }}>
            <span className="mono" style={{ color: 'var(--gold)', fontSize: 12, letterSpacing: '0.16em' }}>{it.code}</span>
            <span style={{ opacity: 0.7 }}>{it.en}</span>
            <span style={{ width: 4, height: 4, borderRadius: '50%', background: 'var(--vermillion)', marginLeft: 32 }} />
          </span>
        ))}
      </div>
      <style>{`
        @keyframes marquee {
          0% { transform: translateX(0); }
          100% { transform: translateX(-33.333%); }
        }
      `}</style>
    </div>
  );
}

// ============ Stats / trust bar ============
function StatsBar() {
  const ref = useRefM(null);
  const [trigger, setTrigger] = useStateM(false);
  useEffectM(() => {
    const io = new IntersectionObserver(([e]) => {
      if (e.isIntersecting) { setTrigger(true); io.disconnect(); }
    }, { threshold: 0.4 });
    if (ref.current) io.observe(ref.current);
    return () => io.disconnect();
  }, []);

  const stats = [
    { n: 12400, suffix: '+', label: 'Containers shipped', sub: '24-month rolling' },
    { n: 2400, suffix: '', label: 'Vetted Chinese suppliers', sub: 'Across 14 provinces' },
    { n: 68, suffix: '', label: 'Active trade routes', sub: 'Sea · Air · Land' },
    { n: 99.2, suffix: '%', label: 'On-time delivery', sub: 'Q1 2026 average', decimal: 1 },
  ];

  return (
    <section ref={ref} style={{
      background: 'var(--paper)',
      padding: '120px 0 100px',
      position: 'relative',
    }}>
      <div className="container">
        <div className="reveal" style={{ maxWidth: 720, marginBottom: 72 }}>
          <div className="eyebrow" style={{ marginBottom: 18 }}>By the Numbers</div>
          <h2 className="display" style={{ fontSize: 'clamp(36px, 4.4vw, 56px)' }}>
            A decade of moving goods across <em style={{ fontStyle: 'italic', color: 'var(--vermillion)' }}>five continents</em>.
          </h2>
          <div className="reveal-line" style={{
            height: 1, background: 'var(--ink)', marginTop: 32, opacity: 0.3,
          }} />
        </div>
        <div style={{
          display: 'grid', gridTemplateColumns: 'repeat(4, 1fr)', gap: 0,
          borderTop: '1px solid var(--line-strong)',
          borderBottom: '1px solid var(--line-strong)',
        }} className="stats-grid">
          {stats.map((s, i) => (
            <StatCell key={i} stat={s} trigger={trigger} index={i} />
          ))}
        </div>
      </div>
      <style>{`
        @media (max-width: 860px) {
          .stats-grid { grid-template-columns: repeat(2, 1fr) !important; }
        }
      `}</style>
    </section>
  );
}

function StatCell({ stat, trigger, index }) {
  const val = useCountUp(stat.n * (stat.decimal ? 10 : 1), trigger, 1800 + index * 150);
  const display = stat.decimal ? (val / 10).toFixed(stat.decimal) : val.toLocaleString();
  return (
    <div className={`reveal reveal-delay-${index + 1}`} style={{
      padding: '40px 32px',
      borderRight: index < 3 ? '1px solid var(--line)' : 'none',
    }}>
      <div className="display tabular" style={{
        fontSize: 'clamp(40px, 4.5vw, 64px)', color: 'var(--ink)', fontWeight: 500,
      }}>
        {display}<span style={{ color: 'var(--vermillion)' }}>{stat.suffix}</span>
      </div>
      <div style={{ fontSize: 14, fontWeight: 600, color: 'var(--ink)', marginTop: 12 }}>
        {stat.label}
      </div>
      <div style={{ fontSize: 12, color: 'var(--ash)', marginTop: 4 }}>
        {stat.sub}
      </div>
    </div>
  );
}

Object.assign(window, { Marquee, StatsBar });
