// hardware.jsx — Raseed onboarding · hardware illustrations & diagrams
// Exports: BridgeDevice, TileDevice, POSDevice, PrinterDevice, BridgeDiagram,
//          TileDiagram, PhoneTap, Confetti
// NOTE: Tile/Bridge art are tasteful placeholders — swap in real 2D product
// renders when supplied. Brand-faced so they read correctly in context.

// ── Print Bridge: a small USB pass-through adapter ──────────────
function BridgeDevice({ w = 150, dark = false }) {
  const body = dark ? '#1a1a14' : '#27271f';
  return (
    <svg width={w} height={w * 0.62} viewBox="0 0 150 93" fill="none">
      <defs>
        <linearGradient id="brg" x1="0" y1="0" x2="0" y2="1">
          <stop offset="0" stopColor="#3d3d38" /><stop offset="1" stopColor={body} />
        </linearGradient>
      </defs>
      {/* body */}
      <rect x="30" y="22" width="90" height="50" rx="11" fill="url(#brg)" />
      <rect x="30" y="22" width="90" height="50" rx="11" stroke="rgba(255,255,255,0.08)" />
      {/* green status LED */}
      <circle cx="75" cy="38" r="4.5" fill="#4ade80" />
      <circle cx="75" cy="38" r="8" fill="#4ade80" opacity="0.25" />
      {/* raseed chevron emboss */}
      <path d="M62 58 L75 48 L88 58" stroke="rgba(255,255,255,0.4)" strokeWidth="3" strokeLinecap="round" strokeLinejoin="round" />
      {/* USB plug left (into POS) */}
      <rect x="6" y="40" width="26" height="14" rx="2.5" fill="#a3a398" />
      <rect x="2" y="43" width="6" height="8" rx="1.5" fill="#72726a" />
      {/* USB port right (printer plugs in) */}
      <rect x="118" y="40" width="22" height="14" rx="2.5" fill="#0f0f0b" />
      <rect x="122" y="44" width="14" height="6" rx="1" fill="#3d3d38" />
    </svg>
  );
}

// ── The Tile: counter NFC pad, raseed-faced, optional live ripple ──
function TileDevice({ w = 150, glow = false, ripple = false }) {
  return (
    <svg width={w} height={w} viewBox="0 0 150 150" fill="none">
      <defs>
        <linearGradient id="tileFace" x1="0" y1="0" x2="0.4" y2="1">
          <stop offset="0" stopColor="#16a34a" /><stop offset="1" stopColor="#14532d" />
        </linearGradient>
        <radialGradient id="tileGlow" cx="0.5" cy="0.4" r="0.6">
          <stop offset="0" stopColor="#4ade80" stopOpacity="0.55" /><stop offset="1" stopColor="#4ade80" stopOpacity="0" />
        </radialGradient>
      </defs>
      {glow && <circle cx="75" cy="72" r="70" fill="url(#tileGlow)" />}
      {/* base shadow */}
      <ellipse cx="75" cy="132" rx="52" ry="9" fill="rgba(15,15,11,0.18)" />
      {/* body */}
      <rect x="22" y="24" width="106" height="106" rx="30" fill="#0f0f0b" />
      <rect x="27" y="28" width="96" height="96" rx="26" fill="url(#tileFace)" />
      <rect x="27" y="28" width="96" height="96" rx="26" fill="none" stroke="rgba(255,255,255,0.12)" />
      {/* NFC ripple rings */}
      {ripple && [0, 1, 2].map(i => (
        <circle key={i} cx="75" cy="62" r="20" fill="none" stroke="#86efac" strokeWidth="2.5" opacity="0">
          <animate attributeName="r" values="14;46" dur="2s" begin={`${i * 0.66}s`} repeatCount="indefinite" />
          <animate attributeName="opacity" values="0.7;0" dur="2s" begin={`${i * 0.66}s`} repeatCount="indefinite" />
        </circle>
      ))}
      {/* raseed mark */}
      <path d="M55 78 L75 58 L95 78" stroke="#fff" strokeWidth="5.5" strokeLinecap="round" strokeLinejoin="round" />
      <circle cx="75" cy="56" r="4" fill="#fff" />
      <line x1="58" y1="94" x2="92" y2="94" stroke="#fff" strokeOpacity="0.55" strokeWidth="3.5" strokeLinecap="round" />
      <line x1="63" y1="103" x2="87" y2="103" stroke="#fff" strokeOpacity="0.3" strokeWidth="3.5" strokeLinecap="round" />
    </svg>
  );
}

// ── POS terminal (card machine) ──
function POSDevice({ w = 90, dark = false }) {
  return (
    <svg width={w} height={w * 1.5} viewBox="0 0 90 135" fill="none">
      <rect x="14" y="2" width="62" height="131" rx="13" fill={dark ? '#27271f' : '#3d3d38'} />
      <rect x="20" y="9" width="50" height="40" rx="5" fill="#86efac" opacity="0.9" />
      <path d="M37 31 L45 23 L53 31" stroke="#14532d" strokeWidth="3" strokeLinecap="round" strokeLinejoin="round" />
      {/* keypad */}
      {[0, 1, 2, 3].map(r => [0, 1, 2].map(c => (
        <rect key={`${r}-${c}`} x={22 + c * 16} y={58 + r * 15} width="11" height="10" rx="2.5" fill="rgba(255,255,255,0.16)" />
      )))}
    </svg>
  );
}

// ── Thermal printer ──
function PrinterDevice({ w = 110, dark = false }) {
  return (
    <svg width={w} height={w * 0.85} viewBox="0 0 110 94" fill="none">
      <rect x="10" y="34" width="90" height="54" rx="10" fill={dark ? '#27271f' : '#575750'} />
      <rect x="20" y="26" width="70" height="16" rx="4" fill={dark ? '#1a1a14' : '#3d3d38'} />
      {/* paper */}
      <rect x="28" y="6" width="54" height="30" rx="3" fill="#fafaf7" />
      <line x1="36" y1="16" x2="74" y2="16" stroke="#d1d1c8" strokeWidth="2.5" strokeLinecap="round" />
      <line x1="36" y1="23" x2="66" y2="23" stroke="#e8e8e1" strokeWidth="2.5" strokeLinecap="round" />
      <circle cx="84" cy="62" r="5" fill="#4ade80" />
    </svg>
  );
}

// ── Step 1 diagram: POS → Bridge → Printer ──
function BridgeDiagram({ dark = false }) {
  const line = dark ? 'rgba(255,255,255,0.22)' : '#d1d1c8';
  const lbl = dark ? 'rgba(255,255,255,0.55)' : '#72726a';
  return (
    <div style={{ position: 'relative', display: 'flex', alignItems: 'flex-end', justifyContent: 'center', gap: 4, padding: '8px 0' }}>
      <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 6 }}>
        <POSDevice w={64} dark={dark} />
        <span style={{ fontSize: 11, fontWeight: 600, color: lbl }}>POS</span>
      </div>
      <svg width="26" height="60" viewBox="0 0 26 60"><path d="M2 30 H24" stroke={line} strokeWidth="2.5" strokeDasharray="3 4" strokeLinecap="round" /></svg>
      <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 6, transform: 'translateY(-6px)' }}>
        <div style={{ filter: 'drop-shadow(0 8px 18px rgba(21,128,61,0.28))' }}><BridgeDevice w={108} dark={dark} /></div>
        <span style={{ fontSize: 11, fontWeight: 700, color: '#16a34a' }}>Print Bridge</span>
      </div>
      <svg width="26" height="60" viewBox="0 0 26 60"><path d="M2 30 H24" stroke={line} strokeWidth="2.5" strokeDasharray="3 4" strokeLinecap="round" /></svg>
      <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 6 }}>
        <PrinterDevice w={78} dark={dark} />
        <span style={{ fontSize: 11, fontWeight: 600, color: lbl }}>Printer</span>
      </div>
    </div>
  );
}

// ── Step 4 diagram: Tile on counter beside terminal ──
function TileDiagram({ dark = false, ripple = true }) {
  const lbl = dark ? 'rgba(255,255,255,0.55)' : '#72726a';
  const counter = dark ? 'rgba(255,255,255,0.07)' : '#f4f4ef';
  const counterEdge = dark ? 'rgba(255,255,255,0.12)' : '#e8e8e1';
  return (
    <div style={{ position: 'relative', width: '100%' }}>
      <div style={{ display: 'flex', alignItems: 'flex-end', justifyContent: 'center', gap: 30, paddingBottom: 14 }}>
        <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 7 }}>
          <POSDevice w={62} dark={dark} />
          <span style={{ fontSize: 11, fontWeight: 600, color: lbl }}>Terminal</span>
        </div>
        <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 7 }}>
          <div style={{ filter: 'drop-shadow(0 10px 22px rgba(21,128,61,0.32))' }}><TileDevice w={118} ripple={ripple} /></div>
          <span style={{ fontSize: 11, fontWeight: 700, color: '#16a34a' }}>Raseed Tile</span>
        </div>
      </div>
      {/* counter surface */}
      <div style={{ height: 10, background: counter, borderTop: `2px solid ${counterEdge}`, borderRadius: '2px 2px 0 0' }} />
    </div>
  );
}

// ── Step 5: phone tapping the Tile (NFC) ──
function PhoneTap({ dark = false }) {
  return (
    <svg width="220" height="180" viewBox="0 0 220 180" fill="none">
      {/* Tile */}
      <g transform="translate(40,52)">
        <rect x="0" y="0" width="86" height="86" rx="24" fill="#0f0f0b" />
        <rect x="4" y="4" width="78" height="78" rx="21" fill="#15803d" />
        <path d="M26 52 L43 35 L60 52" stroke="#fff" strokeWidth="4.5" strokeLinecap="round" strokeLinejoin="round" />
        <circle cx="43" cy="33" r="3.5" fill="#fff" />
        <line x1="28" y1="64" x2="58" y2="64" stroke="#fff" strokeOpacity="0.5" strokeWidth="3" strokeLinecap="round" />
      </g>
      {/* NFC waves */}
      {[0, 1, 2].map(i => (
        <path key={i} d="M126 95 q14 -12 0 -36" fill="none" stroke="#16a34a" strokeWidth="3" strokeLinecap="round" opacity="0" transform={`translate(${i * 9},0)`}>
          <animate attributeName="opacity" values="0.8;0" dur="1.6s" begin={`${i * 0.4}s`} repeatCount="indefinite" />
        </path>
      ))}
      {/* phone */}
      <g transform="translate(150,30) rotate(12)">
        <rect x="0" y="0" width="58" height="116" rx="14" fill={dark ? '#27271f' : '#0f0f0b'} />
        <rect x="4" y="4" width="50" height="108" rx="11" fill="#fafaf7" />
        <rect x="12" y="18" width="34" height="6" rx="3" fill="#15803d" />
        <rect x="12" y="30" width="34" height="44" rx="5" fill="#f0fdf4" />
        <path d="M22 54 L29 47 L36 54" stroke="#15803d" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round" />
        <rect x="12" y="80" width="34" height="4" rx="2" fill="#e8e8e1" />
        <rect x="12" y="88" width="24" height="4" rx="2" fill="#e8e8e1" />
      </g>
    </svg>
  );
}

// ── Confetti for the success screen ──
function Confetti({ count = 28 }) {
  const colors = ['#15803d', '#4ade80', '#86efac', '#f59e0b', '#22c55e', '#bbf7d0'];
  const pieces = React.useMemo(() => Array.from({ length: count }).map((_, i) => ({
    left: Math.random() * 100, delay: Math.random() * 0.5, dur: 1.8 + Math.random() * 1.4,
    color: colors[i % colors.length], size: 6 + Math.random() * 6, rot: Math.random() * 360,
    drift: (Math.random() - 0.5) * 60,
  })), [count]);
  return (
    <div style={{ position: 'absolute', inset: 0, overflow: 'hidden', pointerEvents: 'none', zIndex: 30 }}>
      <style>{`@keyframes confFall{0%{transform:translateY(-40px) rotate(0deg);opacity:0}10%{opacity:1}100%{transform:translateY(780px) translateX(var(--dx)) rotate(720deg);opacity:0}}`}</style>
      {pieces.map((p, i) => (
        <div key={i} style={{
          position: 'absolute', top: 0, left: `${p.left}%`, width: p.size, height: p.size * 0.6,
          background: p.color, borderRadius: 1, '--dx': `${p.drift}px`,
          animation: `confFall ${p.dur}s cubic-bezier(0.3,0.6,0.5,1) ${p.delay}s infinite`,
        }} />
      ))}
    </div>
  );
}

Object.assign(window, {
  BridgeDevice, TileDevice, POSDevice, PrinterDevice,
  BridgeDiagram, TileDiagram, PhoneTap, Confetti,
});
