// product-visuals.jsx — SVG placeholder visuals for FlowFire products
// Dark, moody, machined-metal aesthetic. Each product has a distinct silhouette.

const ProductBg = ({ children, accent = false }) => (
  <div style={{
    position: 'relative',
    width: '100%',
    height: '100%',
    background: 'radial-gradient(ellipse at 30% 20%, #1f1f1f 0%, #0e0e0e 70%, #050505 100%)',
    overflow: 'hidden',
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'center',
  }}>
    {/* subtle grain */}
    <div style={{
      position: 'absolute', inset: 0, opacity: 0.06, mixBlendMode: 'overlay',
      backgroundImage: "url(\"data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='120' height='120'><filter id='n'><feTurbulence baseFrequency='0.9' /></filter><rect width='100%25' height='100%25' filter='url(%23n)'/></svg>\")",
    }} />
    {/* side rim light */}
    <div style={{
      position: 'absolute', inset: 0,
      background: 'linear-gradient(115deg, transparent 60%, rgba(255,200,160,0.04) 75%, transparent 100%)',
    }} />
    {accent && (
      <div style={{
        position: 'absolute', top: 16, left: 16,
        width: 6, height: 6, borderRadius: 3, background: '#C8410D',
        boxShadow: '0 0 12px #C8410D',
      }} />
    )}
    {children}
  </div>
);

// Trigger silhouette (curved)
const ProductTriggerCurved = () => (
  <ProductBg accent>
    <svg viewBox="0 0 200 200" style={{ width: '78%', height: '78%' }}>
      <defs>
        <linearGradient id="metal1" x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%" stopColor="#3a3a3a"/>
          <stop offset="40%" stopColor="#2a2a2a"/>
          <stop offset="100%" stopColor="#0e0e0e"/>
        </linearGradient>
        <linearGradient id="rim1" x1="0" y1="0" x2="1" y2="0">
          <stop offset="0%" stopColor="rgba(255,255,255,0)"/>
          <stop offset="50%" stopColor="rgba(255,255,255,0.5)"/>
          <stop offset="100%" stopColor="rgba(255,255,255,0)"/>
        </linearGradient>
      </defs>
      {/* trigger housing */}
      <rect x="40" y="70" width="120" height="60" rx="4" fill="url(#metal1)" stroke="#444" strokeWidth="0.5"/>
      {/* curved trigger shoe */}
      <path d="M 70 110 Q 100 150 130 110 L 128 122 Q 100 158 72 122 Z" fill="#1a1a1a" stroke="#555" strokeWidth="0.7"/>
      <path d="M 72 112 Q 100 148 128 112" stroke="#666" strokeWidth="0.4" fill="none"/>
      {/* pin holes */}
      <circle cx="60" cy="100" r="4" fill="#0a0a0a" stroke="#333" strokeWidth="0.5"/>
      <circle cx="140" cy="100" r="4" fill="#0a0a0a" stroke="#333" strokeWidth="0.5"/>
      <circle cx="60" cy="100" r="2" fill="#1a1a1a"/>
      <circle cx="140" cy="100" r="2" fill="#1a1a1a"/>
      {/* engraved logo */}
      <text x="100" y="92" textAnchor="middle" fill="#888" fontSize="5" fontFamily="monospace" letterSpacing="1">FLOWFIRE</text>
      {/* rim light */}
      <rect x="42" y="71" width="116" height="0.6" fill="url(#rim1)"/>
    </svg>
  </ProductBg>
);

// Safety selector
const ProductSafety = () => (
  <ProductBg>
    <svg viewBox="0 0 200 200" style={{ width: '70%', height: '70%' }}>
      <defs>
        <linearGradient id="metal2" x1="0" y1="0" x2="1" y2="1">
          <stop offset="0%" stopColor="#3c3c3c"/>
          <stop offset="100%" stopColor="#161616"/>
        </linearGradient>
      </defs>
      {/* selector lever */}
      <rect x="60" y="92" width="80" height="16" rx="3" fill="url(#metal2)" stroke="#555" strokeWidth="0.5"/>
      {/* central pivot */}
      <circle cx="100" cy="100" r="22" fill="#1a1a1a" stroke="#555" strokeWidth="0.5"/>
      <circle cx="100" cy="100" r="14" fill="#0a0a0a" stroke="#333" strokeWidth="0.4"/>
      <circle cx="100" cy="100" r="3" fill="#444"/>
      {/* knurled grip */}
      {[...Array(12)].map((_, i) => (
        <line key={i} x1={62 + i*1.5} y1="94" x2={62 + i*1.5} y2="106" stroke="#222" strokeWidth="0.3"/>
      ))}
      {[...Array(12)].map((_, i) => (
        <line key={'r'+i} x1={138 - i*1.5} y1="94" x2={138 - i*1.5} y2="106" stroke="#222" strokeWidth="0.3"/>
      ))}
      <text x="100" y="135" textAnchor="middle" fill="#666" fontSize="4" fontFamily="monospace" letterSpacing="1.5">S7S</text>
    </svg>
  </ProductBg>
);

// ARC-Fire ambi kit
const ProductArcFire = () => (
  <ProductBg accent>
    <svg viewBox="0 0 200 200" style={{ width: '82%', height: '82%' }}>
      <defs>
        <linearGradient id="metal3" x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%" stopColor="#404040"/>
          <stop offset="50%" stopColor="#2a2a2a"/>
          <stop offset="100%" stopColor="#0e0e0e"/>
        </linearGradient>
      </defs>
      {/* main body */}
      <rect x="30" y="65" width="140" height="70" rx="5" fill="url(#metal3)" stroke="#4a4a4a" strokeWidth="0.5"/>
      {/* trigger flat */}
      <path d="M 75 115 L 125 115 L 122 135 L 78 135 Z" fill="#1a1a1a" stroke="#555" strokeWidth="0.5"/>
      {/* ambi levers */}
      <rect x="22" y="88" width="14" height="6" rx="2" fill="#222" stroke="#444" strokeWidth="0.4"/>
      <rect x="164" y="88" width="14" height="6" rx="2" fill="#222" stroke="#444" strokeWidth="0.4"/>
      {/* selector positions */}
      <circle cx="100" cy="78" r="2" fill="#C8410D"/>
      <circle cx="115" cy="85" r="1.4" fill="#444"/>
      <circle cx="85" cy="85" r="1.4" fill="#444"/>
      {/* pin holes */}
      <circle cx="50" cy="100" r="3.5" fill="#0a0a0a" stroke="#333" strokeWidth="0.4"/>
      <circle cx="150" cy="100" r="3.5" fill="#0a0a0a" stroke="#333" strokeWidth="0.4"/>
      <text x="100" y="155" textAnchor="middle" fill="#888" fontSize="5" fontFamily="monospace" letterSpacing="1.5">ARC-FIRE V2</text>
    </svg>
  </ProductBg>
);

// Disruptor
const ProductDisruptor = () => (
  <ProductBg>
    <svg viewBox="0 0 200 200" style={{ width: '80%', height: '80%' }}>
      <defs>
        <linearGradient id="metal4" x1="0" y1="0" x2="1" y2="1">
          <stop offset="0%" stopColor="#4a4a4a"/>
          <stop offset="60%" stopColor="#1f1f1f"/>
          <stop offset="100%" stopColor="#0a0a0a"/>
        </linearGradient>
      </defs>
      {/* angular body */}
      <path d="M 35 75 L 165 75 L 170 100 L 165 130 L 35 130 L 30 100 Z"
            fill="url(#metal4)" stroke="#555" strokeWidth="0.6"/>
      {/* flat trigger */}
      <rect x="78" y="120" width="44" height="22" rx="2" fill="#161616" stroke="#444" strokeWidth="0.5"/>
      <line x1="80" y1="125" x2="120" y2="125" stroke="#666" strokeWidth="0.3"/>
      {/* pins */}
      <circle cx="55" cy="100" r="3" fill="#0a0a0a" stroke="#333" strokeWidth="0.4"/>
      <circle cx="145" cy="100" r="3" fill="#0a0a0a" stroke="#333" strokeWidth="0.4"/>
      <circle cx="100" cy="92" r="2" fill="#0a0a0a"/>
      {/* engraving */}
      <text x="100" y="105" textAnchor="middle" fill="#999" fontSize="6" fontFamily="monospace" letterSpacing="2">DISRUPTOR</text>
      <text x="100" y="113" textAnchor="middle" fill="#555" fontSize="3" fontFamily="monospace" letterSpacing="1">PARTISAN · A2</text>
    </svg>
  </ProductBg>
);

// Generic component
const ProductShoe = () => (
  <ProductBg>
    <svg viewBox="0 0 200 200" style={{ width: '60%', height: '60%' }}>
      <path d="M 50 100 Q 100 145 150 100 L 148 115 Q 100 152 52 115 Z" fill="#222" stroke="#555" strokeWidth="0.6"/>
      <path d="M 52 102 Q 100 142 148 102" stroke="#666" strokeWidth="0.3" fill="none"/>
    </svg>
  </ProductBg>
);

const ProductTools = () => (
  <ProductBg>
    <svg viewBox="0 0 200 200" style={{ width: '70%', height: '70%' }}>
      {/* punches */}
      {[0, 1, 2].map(i => (
        <g key={i} transform={`translate(${60 + i*20}, 50) rotate(${-3 + i*3}, 6, 60)`}>
          <rect x="2" y="0" width="8" height="100" rx="1" fill="#2a2a2a" stroke="#444" strokeWidth="0.4"/>
          <rect x="0" y="0" width="12" height="14" rx="1" fill="#1a1a1a"/>
        </g>
      ))}
      {/* hammer */}
      <g transform="translate(125, 75) rotate(15)">
        <rect x="0" y="0" width="40" height="14" rx="2" fill="#3a3018" stroke="#5a4a30" strokeWidth="0.4"/>
        <rect x="38" y="-6" width="4" height="26" fill="#1a1a1a"/>
      </g>
    </svg>
  </ProductBg>
);

const PRODUCT_VISUALS = {
  'super-safety-curved': ProductTriggerCurved,
  's7s-super-safety': ProductSafety,
  'arc-fire-v2': ProductArcFire,
  'disruptor': ProductDisruptor,
  'curved-shoe': ProductShoe,
  'install-tools': ProductTools,
};

const ProductVisual = ({ id, image }) => {
  if (image) {
    return (
      <div style={{
        width: '100%', height: '100%',
        background: '#0c0c0c',
        display: 'flex', alignItems: 'center', justifyContent: 'center',
      }}>
        <img src={image} alt=""
          style={{ width: '100%', height: '100%', objectFit: 'cover', objectPosition: 'center', display: 'block' }}
          loading="lazy"
        />
      </div>
    );
  }
  const C = PRODUCT_VISUALS[id] || ProductTriggerCurved;
  return <C />;
};

// Hero visual — exploded view + grid lines
const HeroVisual = () => (
  <div style={{ position: 'absolute', inset: 0, overflow: 'hidden' }}>
    <div style={{
      position: 'absolute', inset: 0,
      background: 'radial-gradient(ellipse at 70% 30%, #2a2520 0%, #0e0e0e 55%, #050505 100%)',
    }}/>
    {/* blueprint grid */}
    <svg style={{ position: 'absolute', inset: 0, width: '100%', height: '100%', opacity: 0.18 }}>
      <defs>
        <pattern id="grid" width="32" height="32" patternUnits="userSpaceOnUse">
          <path d="M 32 0 L 0 0 0 32" fill="none" stroke="#C8410D" strokeWidth="0.4"/>
        </pattern>
      </defs>
      <rect width="100%" height="100%" fill="url(#grid)"/>
    </svg>
    {/* Large product silhouette */}
    <div style={{ position: 'absolute', inset: 0, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
      <svg viewBox="0 0 400 300" style={{ width: '120%', height: '85%' }}>
        <defs>
          <linearGradient id="heroMetal" x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%" stopColor="#5a5a5a" stopOpacity="0.95"/>
            <stop offset="50%" stopColor="#2c2c2c"/>
            <stop offset="100%" stopColor="#0a0a0a"/>
          </linearGradient>
          <filter id="glow">
            <feGaussianBlur stdDeviation="1.5"/>
          </filter>
        </defs>
        {/* main body */}
        <rect x="60" y="110" width="280" height="100" rx="6" fill="url(#heroMetal)" stroke="#666" strokeWidth="0.6"/>
        {/* trigger curve */}
        <path d="M 150 190 Q 200 240 250 190 L 246 208 Q 200 250 154 208 Z" fill="#1a1a1a" stroke="#666" strokeWidth="0.5"/>
        {/* selector */}
        <circle cx="200" cy="135" r="14" fill="#1a1a1a" stroke="#555" strokeWidth="0.5"/>
        <rect x="186" y="131" width="28" height="8" rx="2" fill="#2c2c2c" stroke="#555" strokeWidth="0.4"/>
        {/* pins */}
        <circle cx="100" cy="160" r="6" fill="#0a0a0a" stroke="#444" strokeWidth="0.6"/>
        <circle cx="300" cy="160" r="6" fill="#0a0a0a" stroke="#444" strokeWidth="0.6"/>
        {/* accent dot */}
        <circle cx="200" cy="135" r="2" fill="#C8410D" filter="url(#glow)"/>
        {/* label */}
        <text x="200" y="175" textAnchor="middle" fill="#888" fontSize="7" fontFamily="monospace" letterSpacing="3">FLOWFIRE</text>
        <text x="200" y="186" textAnchor="middle" fill="#555" fontSize="3.5" fontFamily="monospace" letterSpacing="1.5">CNC · TX · MIL-SPEC</text>

        {/* spec callouts */}
        <line x1="100" y1="160" x2="40" y2="80" stroke="#C8410D" strokeWidth="0.6" opacity="0.7"/>
        <circle cx="40" cy="80" r="2" fill="#C8410D"/>
        <text x="44" y="76" fill="#C8410D" fontSize="5" fontFamily="monospace" letterSpacing="0.5">±0.0005"</text>

        <line x1="300" y1="160" x2="370" y2="240" stroke="#C8410D" strokeWidth="0.6" opacity="0.7"/>
        <circle cx="370" cy="240" r="2" fill="#C8410D"/>
        <text x="334" y="252" fill="#C8410D" fontSize="5" fontFamily="monospace" letterSpacing="0.5">A2 TOOL STEEL</text>
      </svg>
    </div>
  </div>
);

Object.assign(window, { ProductVisual, HeroVisual });
