// ─────────────────────────────────────────────────────────────────────────
// store-shapes.jsx
// SVG-силуэты упаковки. Один компонент <ProductShape shape color label />
// возвращает аккуратную фронт-проекцию (бутылка / банка / тюбик / спрей /
// пипетка / помпа). Цвет — заливка стекла/пластика; на каждой бутылке
// печатается мини-этикетка с брендом и названием.
// ─────────────────────────────────────────────────────────────────────────

function ProductShape({ shape = 'bottle', color = '#EFE3C8', label = 'ECOPROF', sub = '', style }) {
  const stroke = 'rgba(0,0,0,0.16)';
  const inner = 'rgba(0,0,0,0.06)';
  // Затемнить цвет для контраста этикетки
  const labelColor = '#2a241e';
  const sublabelColor = 'rgba(42,36,30,0.55)';

  const Label = ({ x = 50, y = 60, w = 50, h = 26 }) => (
    <g>
      <rect x={x - w / 2} y={y - h / 2} width={w} height={h} rx="2" fill="rgba(255,255,255,0.78)" stroke={inner} strokeWidth="0.4"/>
      <text x={x} y={y - 2} textAnchor="middle" fill={labelColor}
        fontFamily="Inter Tight, Inter, sans-serif" fontWeight="500" fontSize="6.4" letterSpacing="0.6">
        {label}
      </text>
      {sub && (
        <text x={x} y={y + 7} textAnchor="middle" fill={sublabelColor}
          fontFamily="JetBrains Mono, monospace" fontSize="3.2" letterSpacing="0.4">
          {sub}
        </text>
      )}
    </g>
  );

  if (shape === 'bottle') {
    // классическая бутылка
    return (
      <svg viewBox="0 0 100 130" style={style}>
        <defs>
          <linearGradient id="bg" x1="0" x2="1" y1="0" y2="0">
            <stop offset="0" stopColor={color} stopOpacity="1"/>
            <stop offset="0.55" stopColor={color} stopOpacity="0.85"/>
            <stop offset="1" stopColor={color} stopOpacity="1"/>
          </linearGradient>
        </defs>
        {/* cap */}
        <rect x="38" y="6" width="24" height="16" rx="2" fill="#2a241e"/>
        {/* neck */}
        <rect x="42" y="22" width="16" height="8" fill={color} stroke={stroke} strokeWidth="0.4"/>
        {/* body */}
        <path d="M30 32 Q24 32 24 40 L24 116 Q24 124 32 124 L68 124 Q76 124 76 116 L76 40 Q76 32 70 32 Z" fill="url(#bg)" stroke={stroke} strokeWidth="0.5"/>
        {/* shine */}
        <path d="M30 36 L30 116" stroke="rgba(255,255,255,0.5)" strokeWidth="2" strokeLinecap="round"/>
        <Label x={50} y={76} w={48} h={28}/>
      </svg>
    );
  }

  if (shape === 'jar') {
    return (
      <svg viewBox="0 0 100 100" style={style}>
        <defs>
          <linearGradient id="jg" x1="0" x2="1" y1="0" y2="0">
            <stop offset="0" stopColor={color}/>
            <stop offset="0.55" stopColor={color} stopOpacity="0.85"/>
            <stop offset="1" stopColor={color}/>
          </linearGradient>
        </defs>
        {/* lid */}
        <rect x="12" y="14" width="76" height="18" rx="3" fill="#2a241e"/>
        <rect x="12" y="30" width="76" height="2" fill="rgba(0,0,0,0.3)"/>
        {/* jar body */}
        <rect x="14" y="32" width="72" height="56" rx="4" fill="url(#jg)" stroke={stroke} strokeWidth="0.5"/>
        <path d="M20 36 L20 84" stroke="rgba(255,255,255,0.5)" strokeWidth="2" strokeLinecap="round"/>
        <Label x={50} y={60} w={52} h={22}/>
      </svg>
    );
  }

  if (shape === 'dropper') {
    // пипетка-флакон
    return (
      <svg viewBox="0 0 100 130" style={style}>
        <defs>
          <linearGradient id="dg" x1="0" x2="1" y1="0" y2="0">
            <stop offset="0" stopColor={color}/>
            <stop offset="0.55" stopColor={color} stopOpacity="0.82"/>
            <stop offset="1" stopColor={color}/>
          </linearGradient>
        </defs>
        {/* dropper top */}
        <rect x="40" y="2" width="20" height="12" rx="2" fill="#2a241e"/>
        <rect x="44" y="14" width="12" height="6" fill="#2a241e"/>
        {/* bulb hint */}
        <ellipse cx="50" cy="9" rx="4" ry="2.5" fill="rgba(255,255,255,0.18)"/>
        {/* neck */}
        <rect x="44" y="20" width="12" height="6" fill={color} stroke={stroke} strokeWidth="0.4"/>
        {/* body */}
        <path d="M30 28 Q26 30 26 36 L26 116 Q26 124 34 124 L66 124 Q74 124 74 116 L74 36 Q74 30 70 28 Z" fill="url(#dg)" stroke={stroke} strokeWidth="0.5"/>
        <path d="M30 34 L30 116" stroke="rgba(255,255,255,0.55)" strokeWidth="2" strokeLinecap="round"/>
        <Label x={50} y={76} w={42} h={32}/>
      </svg>
    );
  }

  if (shape === 'tube') {
    return (
      <svg viewBox="0 0 100 130" style={style}>
        <defs>
          <linearGradient id="tg" x1="0" x2="1" y1="0" y2="0">
            <stop offset="0" stopColor={color}/>
            <stop offset="0.55" stopColor={color} stopOpacity="0.82"/>
            <stop offset="1" stopColor={color}/>
          </linearGradient>
        </defs>
        {/* cap (bottom) */}
        <rect x="20" y="112" width="60" height="14" rx="2" fill="#2a241e"/>
        {/* tube body */}
        <path d="M22 18 L78 18 L74 112 L26 112 Z" fill="url(#tg)" stroke={stroke} strokeWidth="0.5"/>
        {/* sealed top */}
        <path d="M22 18 Q22 10 28 8 L72 8 Q78 10 78 18 Z" fill={color} stroke={stroke} strokeWidth="0.4"/>
        <path d="M28 24 L28 108" stroke="rgba(255,255,255,0.55)" strokeWidth="2" strokeLinecap="round"/>
        <Label x={50} y={62} w={48} h={36}/>
      </svg>
    );
  }

  if (shape === 'spray') {
    // мист
    return (
      <svg viewBox="0 0 100 130" style={style}>
        <defs>
          <linearGradient id="sg" x1="0" x2="1" y1="0" y2="0">
            <stop offset="0" stopColor={color}/>
            <stop offset="0.55" stopColor={color} stopOpacity="0.82"/>
            <stop offset="1" stopColor={color}/>
          </linearGradient>
        </defs>
        {/* nozzle */}
        <rect x="34" y="4" width="32" height="10" rx="2" fill="#2a241e"/>
        <rect x="22" y="6" width="14" height="3" rx="1" fill="#2a241e"/>
        {/* collar */}
        <rect x="38" y="14" width="24" height="6" fill="#2a241e"/>
        {/* body */}
        <rect x="26" y="20" width="48" height="104" rx="6" fill="url(#sg)" stroke={stroke} strokeWidth="0.5"/>
        <path d="M30 26 L30 116" stroke="rgba(255,255,255,0.55)" strokeWidth="2" strokeLinecap="round"/>
        <Label x={50} y={68} w={42} h={36}/>
      </svg>
    );
  }

  if (shape === 'pump') {
    return (
      <svg viewBox="0 0 100 130" style={style}>
        <defs>
          <linearGradient id="pg" x1="0" x2="1" y1="0" y2="0">
            <stop offset="0" stopColor={color}/>
            <stop offset="0.55" stopColor={color} stopOpacity="0.82"/>
            <stop offset="1" stopColor={color}/>
          </linearGradient>
        </defs>
        {/* pump nozzle */}
        <rect x="44" y="2" width="12" height="6" fill="#2a241e"/>
        <path d="M22 8 L56 8 L56 14 L46 14 L46 22 L54 22 L54 28 L40 28 L40 14 L22 14 Z" fill="#2a241e"/>
        {/* collar */}
        <rect x="34" y="28" width="32" height="6" fill="#2a241e"/>
        {/* body */}
        <rect x="22" y="34" width="56" height="90" rx="6" fill="url(#pg)" stroke={stroke} strokeWidth="0.5"/>
        <path d="M26 40 L26 116" stroke="rgba(255,255,255,0.55)" strokeWidth="2" strokeLinecap="round"/>
        <Label x={50} y={78} w={46} h={32}/>
      </svg>
    );
  }

  return null;
}

// Минималистическая иконка категории (упрощённая) — для кружков
function CategoryIcon({ shape, color = '#2a241e' }) {
  const s = { fill: 'none', stroke: color, strokeWidth: 1.5, strokeLinecap: 'round', strokeLinejoin: 'round' };
  if (shape === 'pump') return (
    <svg viewBox="0 0 60 80"><g {...s}>
      <path d="M28 4 L36 4 L36 8 L40 8 L40 14 L28 14 L28 8 L24 8 Z"/>
      <rect x="22" y="14" width="20" height="4"/>
      <rect x="16" y="18" width="32" height="56" rx="3"/>
    </g></svg>
  );
  if (shape === 'jar') return (
    <svg viewBox="0 0 60 60"><g {...s}>
      <rect x="8" y="10" width="44" height="10" rx="2"/>
      <rect x="10" y="20" width="40" height="32" rx="2"/>
    </g></svg>
  );
  if (shape === 'dropper') return (
    <svg viewBox="0 0 60 80"><g {...s}>
      <rect x="24" y="3" width="12" height="8"/>
      <rect x="26" y="11" width="8" height="6"/>
      <path d="M16 20 Q16 18 18 18 L42 18 Q44 18 44 20 L44 70 Q44 74 40 74 L20 74 Q16 74 16 70 Z"/>
    </g></svg>
  );
  if (shape === 'tube') return (
    <svg viewBox="0 0 60 80"><g {...s}>
      <path d="M12 12 L48 12 L46 66 L14 66 Z"/>
      <rect x="14" y="66" width="32" height="9" rx="1.5"/>
      <path d="M12 12 Q12 6 18 6 L42 6 Q48 6 48 12"/>
    </g></svg>
  );
  if (shape === 'spray') return (
    <svg viewBox="0 0 60 80"><g {...s}>
      <rect x="22" y="4" width="20" height="7"/>
      <rect x="14" y="6" width="8" height="3"/>
      <rect x="24" y="11" width="16" height="5"/>
      <rect x="16" y="16" width="32" height="58" rx="3"/>
    </g></svg>
  );
  if (shape === 'bottle') return (
    <svg viewBox="0 0 60 80"><g {...s}>
      <rect x="24" y="4" width="12" height="9"/>
      <rect x="26" y="13" width="8" height="5"/>
      <path d="M18 18 Q14 18 14 23 L14 70 Q14 74 18 74 L42 74 Q46 74 46 70 L46 23 Q46 18 42 18 Z"/>
    </g></svg>
  );
  return null;
}

Object.assign(window, { ProductShape, CategoryIcon, ProductMedia });

// ProductMedia — рендерить реальне фото товара (якщо є p.image), інакше
// фолбек на SVG-силует. Обробляє onError → теж переключається на силует.
function ProductMedia({ p, alt = '', fit = 'contain' }) {
  const [failed, setFailed] = React.useState(false);
  if (p && p.image && !failed) {
    return (
      <img
        src={p.image}
        alt={alt || p.name}
        loading="lazy"
        style={{
          width: '100%', height: '100%',
          objectFit: fit,
          display: 'block',
        }}
        onError={() => setFailed(true)}
      />
    );
  }
  // fallback
  return <ProductShape shape={p?.shape || 'bottle'} color={p?.color || '#EFE3C8'} label="ECOPROF" sub={(p?.name || '').toUpperCase().slice(0,12)}/>;
}
