/* SVG illustrations for individual products within a category */

/* Stand bases */
const StandBase = ({ y = 280, w = 80, type = "wood" }) => {
  if (type === "wood") {
    return (
      <ellipse cx="150" cy={y} rx={w/2} ry="8" fill="#7a5230" stroke="#4a3320" strokeWidth="0.8" />
    );
  }
  if (type === "chrome") {
    return (
      <>
        <ellipse cx="150" cy={y} rx={w/2} ry="6" fill="#c8ccd1" />
        <ellipse cx="150" cy={y - 2} rx={w/2 - 4} ry="4" fill="#e8eaee" />
      </>
    );
  }
  return null;
};

/* A small triangle flag attached to a pole */
const SmallFlag = ({ x, poleColor = "#7a5230", flagColor = "#0B2D5C", flagPattern = "il", side = "right", height = 200 }) => {
  // pole from base up to top
  const poleX = x;
  const poleTop = 30;
  const poleBottom = 285;
  const flagW = 60;
  const flagH = 38;
  const dir = side === "right" ? 1 : -1;
  return (
    <g>
      {/* pole */}
      <rect x={poleX - 1} y={poleTop} width="2" height={poleBottom - poleTop} fill={poleColor} />
      {/* finial */}
      <circle cx={poleX} cy={poleTop - 2} r="3" fill="#b8860b" />
      {/* flag */}
      <g transform={`translate(${poleX}, 50)`}>
        {flagPattern === "il" && (
          <g>
            <rect x={dir === 1 ? 0 : -flagW} y="0" width={flagW} height={flagH} fill="#fafbff" stroke="#ddd" strokeWidth="0.3" />
            <rect x={dir === 1 ? 0 : -flagW} y="4" width={flagW} height="4" fill="#0B2D5C" />
            <rect x={dir === 1 ? 0 : -flagW} y={flagH - 8} width={flagW} height="4" fill="#0B2D5C" />
            <g transform={`translate(${dir === 1 ? flagW/2 : -flagW/2}, ${flagH/2}) scale(0.5)`} stroke="#0B2D5C" strokeWidth="1.5" fill="none">
              <polygon points="0,-10 8.66,5 -8.66,5" />
              <polygon points="0,10 8.66,-5 -8.66,-5" />
            </g>
          </g>
        )}
        {flagPattern === "ge" && (
          <g>
            <rect x={dir === 1 ? 0 : -flagW} y="0" width={flagW} height={flagH/3} fill="#1aa64f" />
            <rect x={dir === 1 ? 0 : -flagW} y={flagH/3} width={flagW} height={flagH/3} fill="#ffe700" />
            <rect x={dir === 1 ? 0 : -flagW} y={flagH*2/3} width={flagW} height={flagH/3} fill="#D62839" />
          </g>
        )}
        {flagPattern === "brand" && (
          <g>
            <rect x={dir === 1 ? 0 : -flagW} y="0" width={flagW} height={flagH} fill={flagColor} />
            <circle cx={dir === 1 ? flagW/2 : -flagW/2} cy={flagH/2} r="9" fill="#fafbff" opacity="0.95" />
            <circle cx={dir === 1 ? flagW/2 : -flagW/2} cy={flagH/2} r="4" fill="#D62839" />
          </g>
        )}
        {flagPattern === "stripes" && (
          <g>
            <rect x={dir === 1 ? 0 : -flagW} y="0" width={flagW} height={flagH} fill="#fafbff" stroke="#ddd" strokeWidth="0.3" />
            {[0,1,2,3,4,5].map(i => (
              <rect key={i} x={dir === 1 ? 0 : -flagW} y={i * (flagH/6)} width={flagW} height={flagH/12} fill="#D62839" />
            ))}
            <rect x={dir === 1 ? 0 : -flagW} y="0" width={flagW/2.5} height={flagH/2.2} fill="#0B2D5C" />
          </g>
        )}
      </g>
    </g>
  );
};

/* Single tall pennant flag (with fringe) */
const PennantFlag = ({ x = 150, color = "#1F3A8A" }) => (
  <g>
    <rect x={x - 1} y="20" width="2" height="270" fill="#7a5230" />
    <circle cx={x} cy="18" r="3.5" fill="#b8860b" />
    <g transform={`translate(${x - 50}, 50)`}>
      <rect x="0" y="0" width="100" height="120" fill={color} />
      <rect x="0" y="0" width="100" height="20" fill="#fafbff" />
      <text x="50" y="15" textAnchor="middle" fontSize="9" fontWeight="700" fill="#0B2D5C">RUBIN INT.</text>
      <ellipse cx="50" cy="70" rx="32" ry="18" fill="#0a8ad8" />
      <ellipse cx="50" cy="80" rx="28" ry="6" fill="#1aa64f" opacity="0.7" />
      <circle cx="50" cy="70" r="8" fill="#ffd700" opacity="0.8" />
      {/* fringe */}
      {Array.from({length: 16}).map((_, i) => (
        <line key={i} x1={i * 6 + 3} y1="120" x2={i * 6 + 3} y2="128" stroke="#ffd700" strokeWidth="0.8" />
      ))}
    </g>
  </g>
);

/* Wrapper to render product card scene with light cream background */
const ProductScene = ({ children }) => (
  <svg viewBox="0 0 300 320" xmlns="http://www.w3.org/2000/svg" style={{ width: '100%', height: '100%' }}>
    <defs>
      <radialGradient id="scene-bg" cx="0.5" cy="0.5" r="0.7">
        <stop offset="0%" stopColor="#fff" />
        <stop offset="100%" stopColor="#fadcc7" />
      </radialGradient>
      <radialGradient id="scene-light" cx="0.5" cy="0.55" r="0.4">
        <stop offset="0%" stopColor="#fff" stopOpacity="0.95" />
        <stop offset="100%" stopColor="#fff" stopOpacity="0" />
      </radialGradient>
    </defs>
    <rect width="300" height="320" fill="url(#scene-bg)" />
    <rect width="300" height="320" fill="url(#scene-light)" />
    {children}
    {/* Subtle floor shadow */}
    <ellipse cx="150" cy="290" rx="70" ry="6" fill="#000" opacity="0.08" />
  </svg>
);

/* Predefined product visuals (variants for stand category) */
const Products = {
  StandWoodTwoFlags: () => (
    <ProductScene>
      <SmallFlag x={125} flagPattern="ge" side="left" />
      <SmallFlag x={175} flagPattern="il" side="right" />
      <StandBase y={285} w={70} type="wood" />
    </ProductScene>
  ),
  StandWoodPennantFringe: () => (
    <ProductScene>
      <PennantFlag x={150} color="#1F3A8A" />
      <StandBase y={290} w={65} type="wood" />
    </ProductScene>
  ),
  StandWoodThreeFlags: () => (
    <ProductScene>
      <SmallFlag x={115} flagPattern="stripes" side="left" />
      <SmallFlag x={150} flagPattern="il" side="right" />
      <SmallFlag x={185} flagPattern="brand" side="right" flagColor="#1aa64f" />
      <StandBase y={285} w={90} type="wood" />
    </ProductScene>
  ),
  StandWoodSingleStripes: () => (
    <ProductScene>
      <SmallFlag x={150} flagPattern="stripes" side="right" />
      <StandBase y={285} w={50} type="wood" />
    </ProductScene>
  ),
  StandChromeTwo: () => (
    <ProductScene>
      <g>
        <line x1="140" y1="50" x2="135" y2="280" stroke="#d4d8de" strokeWidth="2" />
        <line x1="160" y1="50" x2="165" y2="280" stroke="#d4d8de" strokeWidth="2" />
        <circle cx="140" cy="48" r="4" fill="#e8eaee" stroke="#a8acb4" strokeWidth="0.5" />
        <circle cx="160" cy="48" r="4" fill="#e8eaee" stroke="#a8acb4" strokeWidth="0.5" />
      </g>
      <StandBase y={285} w={60} type="chrome" />
    </ProductScene>
  ),
  StandChromeSingle: () => (
    <ProductScene>
      <line x1="150" y1="50" x2="150" y2="280" stroke="#d4d8de" strokeWidth="2" />
      <circle cx="150" cy="48" r="4" fill="#e8eaee" stroke="#a8acb4" strokeWidth="0.5" />
      <StandBase y={285} w={50} type="chrome" />
    </ProductScene>
  ),
  StandWoodThreeEmpty: () => (
    <ProductScene>
      <g>
        <line x1="120" y1="50" x2="125" y2="285" stroke="#7a5230" strokeWidth="2" />
        <line x1="150" y1="40" x2="150" y2="285" stroke="#7a5230" strokeWidth="2" />
        <line x1="180" y1="50" x2="175" y2="285" stroke="#7a5230" strokeWidth="2" />
        <polygon points="120,48 113,42 113,52" fill="#b8860b" />
        <polygon points="150,38 143,32 143,42" fill="#b8860b" />
        <polygon points="180,48 187,42 187,52" fill="#b8860b" />
      </g>
      <StandBase y={290} w={90} type="wood" />
    </ProductScene>
  ),
  StandWoodTwoFlagsB: () => (
    <ProductScene>
      <SmallFlag x={130} flagPattern="brand" side="left" flagColor="#1F3A8A" />
      <SmallFlag x={170} flagPattern="il" side="right" />
      <SmallFlag x={150} flagPattern="ge" side="left" />
      <StandBase y={285} w={80} type="wood" />
    </ProductScene>
  ),
  StandChromeSlim: () => (
    <ProductScene>
      <line x1="150" y1="55" x2="150" y2="280" stroke="#b8bcc4" strokeWidth="1.5" />
      <circle cx="150" cy="52" r="3" fill="#e8eaee" />
      <ellipse cx="150" cy="282" rx="35" ry="5" fill="#c8ccd1" />
      <ellipse cx="150" cy="280" rx="30" ry="3" fill="#e8eaee" />
    </ProductScene>
  ),
  StandWoodLarge3Strip: () => (
    <ProductScene>
      <SmallFlag x={120} flagPattern="ge" side="left" />
      <SmallFlag x={150} flagPattern="il" side="right" />
      <SmallFlag x={180} flagPattern="stripes" side="right" />
      <StandBase y={285} w={95} type="wood" />
    </ProductScene>
  ),
};

Object.assign(window, { Products, ProductScene, SmallFlag, PennantFlag, StandBase });
