/* global React */
// components.jsx — Logo variants, Bottle SVG, Nav, Footer, Marquee, etc.

// ──────────────────────────────────────────────────────────────────────────
// LOGOS — three lockup variations
// ──────────────────────────────────────────────────────────────────────────

function LogoWordmark({ size = 18, weight = 600 }) {
  // Pure wordmark
  return (
    <span style={{
      fontWeight: weight,
      fontSize: size,
      letterSpacing: '-0.02em',
      color: 'var(--ink)',
    }}>
      Protocol
    </span>
  );
}

function LogoTechnical({ size = 18, weight = 600 }) {
  // Wordmark + version metadata
  return (
    <span style={{ display: 'inline-flex', alignItems: 'baseline', gap: 8 }}>
      <span style={{ fontWeight: weight, fontSize: size, letterSpacing: '-0.02em' }}>
        Protocol
      </span>
      <span className="mono" style={{
        fontSize: Math.max(9, size * 0.55),
        color: 'var(--ink-3)',
        letterSpacing: '0.04em',
        textTransform: 'uppercase',
      }}>
        v.01 / 2025
      </span>
    </span>
  );
}

function LogoStamp({ size = 18 }) {
  // Stamp-style: P inside a ring with metadata
  const D = size * 2.4;
  return (
    <span style={{ display: 'inline-flex', alignItems: 'center', gap: 10 }}>
      <svg width={D} height={D} viewBox="0 0 48 48" aria-hidden="true">
        <circle cx="24" cy="24" r="22.5" fill="none" stroke="currentColor" strokeWidth="1" />
        <circle cx="24" cy="24" r="18" fill="none" stroke="currentColor" strokeWidth="0.5" />
        <text x="24" y="29" textAnchor="middle" fontFamily="var(--sans)" fontSize="17" fontWeight="600" letterSpacing="-0.02em" fill="currentColor">P</text>
        {/* tick marks */}
        {[0, 90, 180, 270].map((d) => (
          <line key={d}
            x1="24" y1="0.5" x2="24" y2="3.5"
            stroke="currentColor" strokeWidth="0.8"
            transform={`rotate(${d} 24 24)`} />
        ))}
      </svg>
      <span style={{ fontWeight: 600, fontSize: size, letterSpacing: '-0.02em' }}>Protocol</span>
    </span>
  );
}

function Logo({ variant = 'wordmark', size = 18 }) {
  if (variant === 'technical') return <LogoTechnical size={size} />;
  if (variant === 'stamp') return <LogoStamp size={size} />;
  return <LogoWordmark size={size} />;
}

// ──────────────────────────────────────────────────────────────────────────
// BOTTLE — schematic vector bottle with full label
// ──────────────────────────────────────────────────────────────────────────

function Bottle({ product, h = 280, dark = false, asLabel = false }) {
  // Bottle is drawn at a base height of 360, scaled to h.
  // Width is proportional.
  const W = 140, H = 360;
  const scale = h / H;

  const bg = dark ? '#1c1c1c' : '#f7f6f3';
  const stroke = dark ? '#444' : '#0a0a0a';
  const labelBg = dark ? '#0a0a0a' : '#fafaf7';
  const labelInk = dark ? '#fafafa' : '#0a0a0a';
  const labelMid = dark ? 'rgba(250,250,250,.55)' : 'rgba(10,10,10,.55)';

  return (
    <svg width={W * scale} height={H * scale} viewBox={`0 0 ${W} ${H}`}
         style={{ display: 'block' }} aria-hidden="true">
      <defs>
        <linearGradient id={`gloss-${product.id}`} x1="0" y1="0" x2="1" y2="0">
          <stop offset="0%" stopColor="rgba(255,255,255,.0)" />
          <stop offset="40%" stopColor="rgba(255,255,255,.18)" />
          <stop offset="50%" stopColor="rgba(255,255,255,.0)" />
          <stop offset="90%" stopColor="rgba(0,0,0,.06)" />
        </linearGradient>
      </defs>

      {/* pump cap */}
      <rect x="56" y="6" width="28" height="14" fill={stroke} />
      <rect x="56" y="20" width="28" height="4" fill={dark ? '#666' : '#333'} />
      {/* nozzle */}
      <path d="M70 24 L70 38 L96 38 L96 42 L62 42 L62 24 Z" fill={stroke} />

      {/* shoulder */}
      <path d={`M20 60 Q20 42 30 42 L110 42 Q120 42 120 60 L120 ${H - 8} Q120 ${H - 2} 114 ${H - 2} L26 ${H - 2} Q20 ${H - 2} 20 ${H - 8} Z`}
            fill={bg} stroke={stroke} strokeWidth="0.8" />

      {/* gloss */}
      <path d={`M20 60 Q20 42 30 42 L110 42 Q120 42 120 60 L120 ${H - 8} Q120 ${H - 2} 114 ${H - 2} L26 ${H - 2} Q20 ${H - 2} 20 ${H - 8} Z`}
            fill={`url(#gloss-${product.id})`} />

      {/* label area */}
      <rect x="28" y="92" width="84" height="220" fill={labelBg} stroke={stroke} strokeWidth="0.5" />

      {/* top hairline */}
      <line x1="32" y1="106" x2="108" y2="106" stroke={labelInk} strokeWidth="0.4" />
      <line x1="32" y1="298" x2="108" y2="298" stroke={labelInk} strokeWidth="0.4" />

      {/* meta top */}
      <text x="32" y="103" fontFamily="var(--mono)" fontSize="3.6" fill={labelMid} letterSpacing="0.4">PROTOCOL · LONDON</text>
      <text x="108" y="103" textAnchor="end" fontFamily="var(--mono)" fontSize="3.6" fill={labelMid} letterSpacing="0.4">{product.batch}</text>

      {/* big number */}
      <text x="32" y="148" fontFamily="var(--sans)" fontSize="34" fontWeight="500" fill={labelInk} letterSpacing="-1.6">
        {product.id}
      </text>
      <text x="68" y="125" fontFamily="var(--mono)" fontSize="3.6" fill={labelMid} letterSpacing="0.4">FORMULA</text>
      <text x="68" y="132" fontFamily="var(--mono)" fontSize="3.6" fill={labelMid} letterSpacing="0.4">N{'\u00B0'} {product.id}</text>

      {/* product name */}
      <text x="32" y="172" fontFamily="var(--sans)" fontSize="8" fontWeight="500" fill={labelInk} letterSpacing="-0.2">
        {product.name.split(' ').slice(0, 2).join(' ')}
      </text>
      {product.name.split(' ').length > 2 && (
        <text x="32" y="180" fontFamily="var(--sans)" fontSize="8" fontWeight="500" fill={labelInk} letterSpacing="-0.2">
          {product.name.split(' ').slice(2).join(' ')}
        </text>
      )}

      {/* divider */}
      <line x1="32" y1="190" x2="108" y2="190" stroke={labelInk} strokeWidth="0.3" strokeDasharray="1,1" />

      {/* actives */}
      <text x="32" y="200" fontFamily="var(--mono)" fontSize="3.4" fill={labelMid} letterSpacing="0.3">ACTIVE INGREDIENTS</text>
      {wrapText(product.actives, 32).map((line, i) => (
        <text key={i} x="32" y={209 + i * 5} fontFamily="var(--mono)" fontSize="4.2" fill={labelInk}>
          {line}
        </text>
      ))}

      {/* divider */}
      <line x1="32" y1="232" x2="108" y2="232" stroke={labelInk} strokeWidth="0.3" strokeDasharray="1,1" />

      {/* spec rows */}
      <text x="32" y="241" fontFamily="var(--mono)" fontSize="3.4" fill={labelMid} letterSpacing="0.3">VOL</text>
      <text x="108" y="241" textAnchor="end" fontFamily="var(--mono)" fontSize="3.8" fill={labelInk}>{product.capacity}</text>

      <text x="32" y="249" fontFamily="var(--mono)" fontSize="3.4" fill={labelMid} letterSpacing="0.3">pH</text>
      <text x="108" y="249" textAnchor="end" fontFamily="var(--mono)" fontSize="3.8" fill={labelInk}>{product.ph}</text>

      <text x="32" y="257" fontFamily="var(--mono)" fontSize="3.4" fill={labelMid} letterSpacing="0.3">DOSE</text>
      <text x="108" y="257" textAnchor="end" fontFamily="var(--mono)" fontSize="3.8" fill={labelInk}>{product.dosage.split(' ')[0]} {product.dosage.split(' ')[1]}</text>

      <text x="32" y="265" fontFamily="var(--mono)" fontSize="3.4" fill={labelMid} letterSpacing="0.3">TEMP</text>
      <text x="108" y="265" textAnchor="end" fontFamily="var(--mono)" fontSize="3.8" fill={labelInk}>{product.temp}</text>

      {/* big formula number bottom */}
      <text x="70" y="290" textAnchor="middle" fontFamily="var(--mono)" fontSize="14" fontWeight="500" fill={labelInk} letterSpacing="1">
        {product.id}
      </text>

      {/* bottom meta */}
      <text x="32" y="307" fontFamily="var(--mono)" fontSize="3.2" fill={labelMid} letterSpacing="0.3">SCAN FOR FULL SDS</text>
      <text x="108" y="307" textAnchor="end" fontFamily="var(--mono)" fontSize="3.2" fill={labelMid} letterSpacing="0.3">REFILL READY</text>
    </svg>
  );
}

// crude word-wrap for SVG labels
function wrapText(str, maxChars) {
  const words = String(str).split(' ');
  const lines = [];
  let cur = '';
  for (const w of words) {
    if ((cur + ' ' + w).trim().length > maxChars) {
      lines.push(cur);
      cur = w;
    } else {
      cur = (cur + ' ' + w).trim();
    }
  }
  if (cur) lines.push(cur);
  return lines.slice(0, 3);
}

// ──────────────────────────────────────────────────────────────────────────
// REFILL POUCH — secondary packaging
// ──────────────────────────────────────────────────────────────────────────

function Pouch({ product, h = 280 }) {
  const W = 180, H = 280;
  const scale = h / H;
  return (
    <svg width={W * scale} height={H * scale} viewBox={`0 0 ${W} ${H}`}>
      {/* main pouch body — slightly trapezoid */}
      <path d="M22 28 L158 28 L162 268 L18 268 Z"
            fill="#f1f0eb" stroke="#0a0a0a" strokeWidth="0.6" />
      {/* sealed crimp top */}
      <rect x="22" y="20" width="136" height="10" fill="#eae8e1" stroke="#0a0a0a" strokeWidth="0.5" />
      {/* notch */}
      <path d="M148 20 L154 14 L158 14 L158 20 Z" fill="#0a0a0a" />
      {/* label content */}
      <text x="32" y="50" fontFamily="var(--mono)" fontSize="5" fill="rgba(0,0,0,.55)" letterSpacing="0.5">REFILL · 1L</text>
      <text x="32" y="58" fontFamily="var(--mono)" fontSize="5" fill="rgba(0,0,0,.55)" letterSpacing="0.5">{product.batch}</text>

      <text x="32" y="110" fontFamily="var(--sans)" fontSize="44" fontWeight="500" fill="#0a0a0a" letterSpacing="-2">
        {product.id}
      </text>
      <text x="32" y="135" fontFamily="var(--sans)" fontSize="11" fontWeight="500" fill="#0a0a0a" letterSpacing="-0.3">
        {product.name}
      </text>

      <line x1="32" y1="148" x2="148" y2="148" stroke="#0a0a0a" strokeWidth="0.3" strokeDasharray="1,1" />

      {wrapText(product.actives, 28).map((line, i) => (
        <text key={i} x="32" y={162 + i * 7} fontFamily="var(--mono)" fontSize="5.5" fill="#0a0a0a">
          {line}
        </text>
      ))}

      <line x1="32" y1="200" x2="148" y2="200" stroke="#0a0a0a" strokeWidth="0.3" strokeDasharray="1,1" />

      <text x="32" y="215" fontFamily="var(--mono)" fontSize="4.5" fill="rgba(0,0,0,.55)" letterSpacing="0.4">PROTOCOL · LONDON · 2026</text>
      <text x="32" y="225" fontFamily="var(--mono)" fontSize="4.5" fill="rgba(0,0,0,.55)" letterSpacing="0.4">FOR USE WITH STARTER VESSEL N°{product.id}</text>

      <text x="90" y="258" textAnchor="middle" fontFamily="var(--mono)" fontSize="9" fontWeight="500" fill="#0a0a0a" letterSpacing="2">REFILL</text>
    </svg>
  );
}

// ──────────────────────────────────────────────────────────────────────────
// MOLECULAR / MECHANISM DIAGRAM
// ──────────────────────────────────────────────────────────────────────────

function MechanismDiagram() {
  // Generic enzyme-cleavage schematic
  return (
    <svg viewBox="0 0 600 280" style={{ width: '100%', height: 'auto' }}>
      {/* fibre */}
      <text x="20" y="36" fontFamily="var(--mono)" fontSize="11" fill="var(--ink-3)" letterSpacing="0.5">[1] BOUND SOIL</text>
      <rect x="20" y="50" width="160" height="40" fill="none" stroke="currentColor" strokeWidth="0.6" />
      <text x="100" y="74" textAnchor="middle" fontFamily="var(--mono)" fontSize="12" fill="currentColor">FIBRE SUBSTRATE</text>
      {/* soil molecule */}
      <circle cx="100" cy="120" r="14" fill="none" stroke="currentColor" strokeWidth="0.8" />
      <circle cx="80" cy="135" r="10" fill="none" stroke="currentColor" strokeWidth="0.8" />
      <circle cx="125" cy="130" r="12" fill="none" stroke="currentColor" strokeWidth="0.8" />
      <line x1="100" y1="90" x2="100" y2="106" stroke="currentColor" strokeWidth="0.6" />
      <line x1="80" y1="130" x2="100" y2="120" stroke="currentColor" strokeWidth="0.5" />
      <line x1="125" y1="125" x2="100" y2="120" stroke="currentColor" strokeWidth="0.5" />
      <text x="100" y="170" textAnchor="middle" fontFamily="var(--mono)" fontSize="9" fill="var(--ink-3)" letterSpacing="0.4">PROTEINACEOUS SOIL</text>

      {/* arrow 1 */}
      <line x1="200" y1="140" x2="240" y2="140" stroke="currentColor" strokeWidth="0.6" />
      <path d="M240 140 L234 137 L234 143 Z" fill="currentColor" />
      <text x="220" y="132" textAnchor="middle" fontFamily="var(--mono)" fontSize="9" fill="var(--ink-3)" letterSpacing="0.4">PROTEASE</text>

      {/* enzyme step */}
      <text x="260" y="36" fontFamily="var(--mono)" fontSize="11" fill="var(--ink-3)" letterSpacing="0.5">[2] HYDROLYSIS</text>
      <rect x="260" y="50" width="160" height="40" fill="none" stroke="currentColor" strokeWidth="0.6" />
      <text x="340" y="74" textAnchor="middle" fontFamily="var(--mono)" fontSize="12" fill="currentColor">FIBRE SUBSTRATE</text>
      <circle cx="320" cy="120" r="10" fill="none" stroke="currentColor" strokeWidth="0.8" />
      <circle cx="360" cy="135" r="8" fill="none" stroke="currentColor" strokeWidth="0.8" />
      <text x="340" y="170" textAnchor="middle" fontFamily="var(--mono)" fontSize="9" fill="var(--ink-3)" letterSpacing="0.4">PEPTIDE FRAGMENTS</text>
      {/* scissors line */}
      <line x1="305" y1="118" x2="335" y2="138" stroke="var(--accent)" strokeWidth="0.7" strokeDasharray="2,1.5" />

      {/* arrow 2 */}
      <line x1="440" y1="140" x2="480" y2="140" stroke="currentColor" strokeWidth="0.6" />
      <path d="M480 140 L474 137 L474 143 Z" fill="currentColor" />
      <text x="460" y="132" textAnchor="middle" fontFamily="var(--mono)" fontSize="9" fill="var(--ink-3)" letterSpacing="0.4">SUSPEND</text>

      {/* free state */}
      <text x="500" y="36" fontFamily="var(--mono)" fontSize="11" fill="var(--ink-3)" letterSpacing="0.5">[3] RINSE</text>
      <rect x="500" y="50" width="80" height="40" fill="none" stroke="currentColor" strokeWidth="0.6" />
      <text x="540" y="74" textAnchor="middle" fontFamily="var(--mono)" fontSize="12" fill="currentColor">CLEAN</text>
      <text x="540" y="170" textAnchor="middle" fontFamily="var(--mono)" fontSize="9" fill="var(--ink-3)" letterSpacing="0.4">SOIL → SOLUTION</text>

      {/* baseline */}
      <line x1="20" y1="220" x2="580" y2="220" stroke="currentColor" strokeWidth="0.4" />
      <text x="20" y="240" fontFamily="var(--mono)" fontSize="10" fill="var(--ink-3)" letterSpacing="0.5">FIG. 01 / MECHANISM OF ACTION</text>
      <text x="580" y="240" textAnchor="end" fontFamily="var(--mono)" fontSize="10" fill="var(--ink-3)" letterSpacing="0.5">PROTOCOL 01 / PROTEASE-BASED HYDROLYSIS</text>
    </svg>
  );
}

// ──────────────────────────────────────────────────────────────────────────
// COMPOSITION BAR — stacked horizontal bar showing % composition
// ──────────────────────────────────────────────────────────────────────────

function CompositionBar({ pct, accent }) {
  const entries = Object.entries(pct);
  const total = entries.reduce((a, [, v]) => a + v, 0);
  return (
    <div>
      <div style={{
        display: 'flex',
        height: 28,
        border: '1px solid var(--ink)',
        marginBottom: 16,
      }}>
        {entries.map(([k, v], i) => (
          <div key={k} style={{
            width: `${(v / total) * 100}%`,
            background: i === 1 ? accent : i % 2 === 0 ? 'var(--bg-soft)' : 'var(--ink)',
            opacity: i % 2 === 0 ? 1 : 0.92,
            borderRight: i < entries.length - 1 ? '1px solid var(--ink)' : 'none',
          }} />
        ))}
      </div>
      <div className="col gap-xs">
        {entries.map(([k, v], i) => (
          <div key={k} className="leader">
            <span style={{
              width: 10, height: 10, marginRight: 2,
              background: i === 1 ? accent : i % 2 === 0 ? 'var(--bg-soft)' : 'var(--ink)',
              border: '1px solid var(--ink)',
            }} />
            <span>{k.replace(/([A-Z])/g, ' $1').toUpperCase()}</span>
            <span className="dots" />
            <span className="tnum">{v.toFixed(1)}%</span>
          </div>
        ))}
      </div>
    </div>
  );
}

// ──────────────────────────────────────────────────────────────────────────
// MARQUEE
// ──────────────────────────────────────────────────────────────────────────

function Marquee({ items }) {
  const track = items.map((item, i) => (
    <span key={i} style={{ display: 'inline-flex', gap: 48 }}>
      <span>{item}</span>
      <span style={{ color: 'var(--ink-3)' }}>/</span>
    </span>
  ));
  return (
    <div className="marquee">
      <div className="marquee-track">{track}</div>
      <div className="marquee-track" aria-hidden="true">{track}</div>
    </div>
  );
}

// ──────────────────────────────────────────────────────────────────────────
// NAV + FOOTER
// ──────────────────────────────────────────────────────────────────────────

function Nav({ route, setRoute, logoVariant, dense }) {
  const links = [
    { id: 'home', label: 'Index' },
    { id: 'products', label: 'Products' },
    { id: 'ingredients', label: 'Ingredients' },
    { id: 'journal', label: 'Journal' },
    { id: 'identity', label: 'Identity' },
  ];
  return (
    <header className="nav">
      <div className="shell">
        <div className={'nav-inner' + (dense ? ' dense' : '')}>
          <a href="#home" onClick={(e) => { e.preventDefault(); setRoute('home'); }}
             className="nav-mark">
            <Logo variant={logoVariant} size={17} />
          </a>
          <nav className="nav-links">
            {links.slice(1).map((l) => (
              <a key={l.id}
                 href={'#' + l.id}
                 className={route.startsWith(l.id) ? 'active' : ''}
                 onClick={(e) => { e.preventDefault(); setRoute(l.id); }}>
                {l.label}
              </a>
            ))}
          </nav>
          <div className="nav-right">
            <span className="meta">EU · GBP</span>
            <span className="pill">CART [00]</span>
          </div>
        </div>
      </div>
    </header>
  );
}

function Footer({ logoVariant }) {
  const cols = [
    { title: 'Products', items: ['01 Biological', '02 Non-Biological', '03 Oxidiser', '04 Conditioner', '05 Pre-Treat', '06 Wool & Silk'] },
    { title: 'Information', items: ['Ingredients Index', 'Mechanisms', 'Compatibility', 'Safety Data Sheets'] },
    { title: 'Protocol', items: ['Journal', 'Refill Service', 'Subscription', 'Trade Accounts'] },
    { title: 'Company', items: ['Manifesto', 'Laboratory', 'Press', 'Contact'] },
  ];
  return (
    <footer className="footer">
      <div className="shell">
        <div className="bar" style={{ marginBottom: 48 }}>
          <span className="strong">PROTOCOL LABORATORIES LTD · LONDON E2</span>
          <span>EST. 2026</span>
          <span>VAT GB 442 8801 02</span>
          <span>BATCH B25/06/A — F</span>
        </div>
        <div className="grid-12" style={{ marginBottom: 64 }}>
          <div style={{ gridColumn: 'span 4' }}>
            <Logo variant={logoVariant} size={28} />
            <p style={{ marginTop: 24, fontSize: 13, color: 'var(--ink-2)', maxWidth: 360, lineHeight: 1.5 }}>
              Highly effective, transparently formulated garment care. No optical brighteners. No dyes. No masking fragrance. Free of decorative claims.
            </p>
            <div className="row gap-sm" style={{ marginTop: 24 }}>
              <span className="tag">ISO 14001</span>
              <span className="tag">A.I.S.E.</span>
              <span className="tag">EWG VERIFIED</span>
            </div>
          </div>
          {cols.map((c) => (
            <div key={c.title} style={{ gridColumn: 'span 2' }}>
              <div className="meta" style={{ marginBottom: 16 }}>{c.title}</div>
              <ul style={{ listStyle: 'none', padding: 0, margin: 0, fontSize: 13, lineHeight: 1.9 }}>
                {c.items.map((i) => <li key={i}><a href="#" style={{ color: 'var(--ink-2)' }}>{i}</a></li>)}
              </ul>
            </div>
          ))}
        </div>
        <hr className="hr" />
        <div className="bar" style={{ border: 'none', paddingTop: 16, color: 'var(--ink-3)' }}>
          <span>© 2026 PROTOCOL LABORATORIES</span>
          <span>PRIVACY · TERMS · COOKIES</span>
          <span>MADE IN UNITED KINGDOM</span>
        </div>
      </div>
    </footer>
  );
}

// ──────────────────────────────────────────────────────────────────────────
// EXPORT
// ──────────────────────────────────────────────────────────────────────────

Object.assign(window, {
  Logo, LogoWordmark, LogoTechnical, LogoStamp,
  Bottle, Pouch, MechanismDiagram, CompositionBar,
  Marquee, Nav, Footer,
});
