// Plassic marketing — shared atoms + helpers (browser globals)
const { useState, useEffect, useRef, useMemo } = React;

// =====================================================
// Score band helpers (per design-system handoff README)
// 80–100 Clean (teal) | 60–79 Low (kraft outlined) | 30–59 Mediocre (coral) | 0–29 High (jungle inverse)
// =====================================================
function bandFor(score) {
  if (score >= 80) return 'clean';
  if (score >= 60) return 'low';
  if (score >= 30) return 'medium';
  return 'high';
}
function verdictFor(score) {
  return { clean: 'CLEAN', low: 'LOW', medium: 'MEDIOCRE', high: 'HIGH' }[bandFor(score)];
}
function pillClassFor(score) {
  return { clean: 'cln', low: 'lo', medium: 'med', high: 'hi' }[bandFor(score)];
}

// =====================================================
// Counting number — per motion spec: 0 → final over 600ms, ease-out cubic
// =====================================================
function CountUp({ to, dur = 600 }) {
  const [n, setN] = useState(0);
  useEffect(() => {
    const start = performance.now();
    let raf;
    const tick = (t) => {
      const p = Math.min(1, (t - start) / dur);
      const eased = 1 - Math.pow(1 - p, 3); // ease-out cubic
      setN(Math.round(to * eased));
      if (p < 1) raf = requestAnimationFrame(tick);
    };
    raf = requestAnimationFrame(tick);
    return () => cancelAnimationFrame(raf);
  }, [to, dur]);
  return <>{n}</>;
}

// =====================================================
// SVG icons (24x24, 1.5px stroke, currentColor) — drop-in
// =====================================================
const Icon = {
  share:    () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"><path d="M12 4v12"/><path d="m7 9 5-5 5 5"/><path d="M5 16v3a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2v-3"/></svg>,
  arrow:    () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"><path d="M5 12h14"/><path d="m13 5 7 7-7 7"/></svg>,
  scan:     () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"><path d="M3 7V5a2 2 0 0 1 2-2h2"/><path d="M17 3h2a2 2 0 0 1 2 2v2"/><path d="M21 17v2a2 2 0 0 1-2 2h-2"/><path d="M7 21H5a2 2 0 0 1-2-2v-2"/><path d="M7 12h10"/></svg>,
  search:   () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"><circle cx="11" cy="11" r="7"/><path d="m20 20-3.5-3.5"/></svg>,
  apple:    () => <svg viewBox="0 0 24 24" fill="currentColor"><path d="M16.4 12.6c0-2.4 2-3.6 2-3.6-1.1-1.6-2.8-1.8-3.4-1.8-1.4-.1-2.8.9-3.5.9-.7 0-1.9-.8-3.1-.8-1.6 0-3.1 1-3.9 2.4-1.7 2.9-.4 7.2 1.2 9.5.8 1.1 1.7 2.4 3 2.4 1.2-.1 1.7-.8 3.1-.8s1.9.8 3.1.8c1.3 0 2.1-1.2 2.9-2.3 1-1.4 1.4-2.7 1.4-2.7s-2.8-1.1-2.8-4zM14.4 5.5c.6-.7 1-1.7.9-2.7-.9 0-1.9.6-2.5 1.3-.6.6-1.1 1.6-1 2.6 1 .1 2-.5 2.6-1.2z"/></svg>,
  android:  () => <svg viewBox="0 0 24 24" fill="currentColor"><path d="M5.7 16h12.6c.5 0 .7-.4.7-.8v-5c0-3.6-3-6.6-6.6-6.7l1-1.8c.1-.2 0-.4-.2-.5-.2-.1-.4 0-.5.2L11.7 3c-1.4 0-3.4 0-4.7.4l-1-1.7c-.1-.2-.3-.3-.5-.2-.2.1-.3.3-.2.5l1 1.8C2.7 4.6 5 9 5 11.7v3.5c0 .4.2.8.7.8zM8 8.4a.7.7 0 1 1 0-1.4.7.7 0 0 1 0 1.4zm8 0a.7.7 0 1 1 0-1.4.7.7 0 0 1 0 1.4zM4 11v6.4c0 .9.7 1.6 1.6 1.6H6v3c0 .8.6 1.4 1.4 1.4S9 22.8 9 22v-3h6v3c0 .8.6 1.4 1.4 1.4S18 22.8 18 22v-3h.4c.9 0 1.6-.7 1.6-1.6V11c-.8 0-1.4.6-1.4 1.4v4.2c0 .2-.2.4-.4.4H5.8c-.2 0-.4-.2-.4-.4v-4.2C5.4 11.6 4.8 11 4 11zm17 .4v4.2c0 .8-.6 1.4-1.4 1.4S18 16.4 18 15.6v-4.2c0-.8.6-1.4 1.4-1.4S21 10.6 21 11.4zM3 11.4v4.2c0 .8-.6 1.4-1.4 1.4S0 16.4 0 15.6v-4.2C0 10.6.6 10 1.4 10S3 10.6 3 11.4z"/></svg>,
  chevR:    () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"><path d="m9 6 6 6-6 6"/></svg>,
  chevL:    () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"><path d="m15 6-6 6 6 6"/></svg>,
  close:    () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"><path d="m6 6 12 12"/><path d="m18 6-12 12"/></svg>,
  plus:     () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"><path d="M12 5v14"/><path d="M5 12h14"/></svg>,
  download: () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"><path d="M12 4v12"/><path d="m7 11 5 5 5-5"/><path d="M5 20h14"/></svg>,
  trendUp:  () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"><path d="m3 17 6-6 4 4 8-8"/><path d="M14 7h7v7"/></svg>,
  send:     () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"><path d="M22 2 11 13"/><path d="M22 2 15 22l-4-9-9-4 20-7z"/></svg>,
  barcode:  () => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"><path d="M4 5v14"/><path d="M7 5v14"/><path d="M10 5v14"/><path d="M14 5v14"/><path d="M17 5v14"/><path d="M20 5v14"/></svg>,
};

// =====================================================
// Plassic wordmark (small, inline)
// =====================================================
function Wordmark({ size = 18 }) {
  // simplified — full marketing wordmark would use the bundled wordmark.svg
  return (
    <span style={{ display: 'inline-flex', alignItems: 'center', gap: 4, fontFamily: 'var(--plassic-font-display)', fontSize: size, letterSpacing: '-1px', textTransform: 'uppercase', lineHeight: 1 }}>
      PLA
      <span style={{ display: 'inline-flex', flexDirection: 'column', justifyContent: 'center', height: size, gap: 1 }}>
        {[0.55, 0.91, 0.7, 0.85, 0.6].map((o, i) => (
          <span key={i} style={{ width: size * 0.55, height: 1.5, background: 'currentColor', opacity: o, transform: `translateX(${i % 2 ? 2 : 0}px)` }} />
        ))}
      </span>
      IC
    </span>
  );
}

// =====================================================
// Doc bar (header on every page) — no fixed positioning per brief
// =====================================================
function DocBar({ crumb, onNav, page }) {
  return (
    <div className="docbar">
      <a className="wm" href="#/" onClick={(e) => { e.preventDefault(); onNav('/'); }} aria-label="Plassic home">
        <Wordmark />
      </a>
      <span className="crumb">{crumb}</span>
    </div>
  );
}

// =====================================================
// Smart App Banner (iOS) — simulated visually
// =====================================================
function SmartBanner({ visible, onDismiss }) {
  if (!visible) return null;
  return (
    <div className="smart-banner">
      <button className="x" onClick={onDismiss} aria-label="Dismiss">
        <svg width="10" height="10" viewBox="0 0 10 10" stroke="currentColor" strokeWidth="1.2"><path d="m2 2 6 6M8 2l-6 6"/></svg>
      </button>
      <div className="icon-tile">
        <svg viewBox="0 0 32 32" fill="none" stroke="#FF5E3A" strokeWidth="2"><rect x="3" y="3" width="26" height="26" rx="6"/><path d="M9 16h14"/><path d="M9 11h14M9 21h14" opacity=".5"/></svg>
      </div>
      <div className="body">
        <strong>Plassic</strong>
        <span style={{ opacity: .8 }}>Free · Plastic scanner</span>
      </div>
      <button className="open">Open</button>
    </div>
  );
}

// =====================================================
// Subnav (page picker) — visible across the site
// =====================================================
function Subnav({ page, onNav }) {
  const items = [
    { p: '/',           l: 'Home' },
    { p: '/wall',       l: 'Brand wall' },
    { p: '/challenge',  l: 'Challenge' },
    { p: '/scan/coles-mascara', l: 'Share page' },
    { p: '/methodology',l: 'Methodology' },
    { p: '/premium',    l: 'Premium' },
  ];
  const isActive = (p) => p === '/' ? page === '/' : page.startsWith(p);
  return (
    <nav className="subnav">
      {items.map(it => (
        <a key={it.p} href={'#' + it.p} className={isActive(it.p) ? 'active' : ''}
           onClick={(e) => { e.preventDefault(); onNav(it.p); }}>{it.l}</a>
      ))}
    </nav>
  );
}

// =====================================================
// Footer
// =====================================================
function Foot({ onNav }) {
  return (
    <footer className="foot">
      <div className="scan-mark">
        <Wordmark size={32} />
      </div>
      <div className="row">
        <div className="col-links">
          <a href="#/methodology" onClick={(e) => { e.preventDefault(); onNav('/methodology'); }}>Methodology</a>
          <a href="#/wall" onClick={(e) => { e.preventDefault(); onNav('/wall'); }}>Brand wall</a>
          <a href="#/challenge" onClick={(e) => { e.preventDefault(); onNav('/challenge'); }}>Pantry challenge</a>
          <a href="#/premium" onClick={(e) => { e.preventDefault(); onNav('/premium'); }}>Premium</a>
        </div>
        <div className="col-links" style={{ alignItems: 'flex-end' }}>
          <a href="mailto:hello@plassic.com">Contact</a>
          <a href="#/methodology" onClick={(e) => { e.preventDefault(); onNav('/methodology'); }}>Sources</a>
          <a href="#/" onClick={(e) => { e.preventDefault(); onNav('/'); }}>App</a>
        </div>
      </div>
      <p className="legal">
        Plassic uses public packaging-content data — see /methodology.<br/>
        We score, we don't sue. © 2026 Plassic Pty Ltd. AU · UK · US · CA.
      </p>
    </footer>
  );
}

// =====================================================
// Receipt-style "QR" (graphic — never a real QR scan target)
// =====================================================
function FauxQR({ size = 44, fg = 'currentColor', bg = 'transparent' }) {
  // deterministic pixel pattern — looks barcode-y but stylized
  const cells = 9;
  const pattern = useMemo(() => {
    const s = "110100111101100101101111010110111011001110011011010110110101111010";
    return Array.from({ length: cells * cells }, (_, i) => s[i % s.length] === '1');
  }, []);
  return (
    <svg width={size} height={size} viewBox={`0 0 ${cells} ${cells}`} style={{ background: bg }}>
      {pattern.map((on, i) => on ? (
        <rect key={i} x={i % cells} y={Math.floor(i / cells)} width="1" height="1" fill={fg} />
      ) : null)}
      {/* Three corner markers */}
      <rect x="0" y="0" width="3" height="3" fill={fg} />
      <rect x="1" y="1" width="1" height="1" fill={bg === 'transparent' ? '#0E2A26' : bg} />
      <rect x={cells - 3} y="0" width="3" height="3" fill={fg} />
      <rect x={cells - 2} y="1" width="1" height="1" fill={bg === 'transparent' ? '#0E2A26' : bg} />
      <rect x="0" y={cells - 3} width="3" height="3" fill={fg} />
      <rect x="1" y={cells - 2} width="1" height="1" fill={bg === 'transparent' ? '#0E2A26' : bg} />
    </svg>
  );
}

// expose
Object.assign(window, {
  bandFor, verdictFor, pillClassFor,
  CountUp, Icon, Wordmark, DocBar, SmartBanner, Subnav, Foot, FauxQR,
});
