// bottle.jsx — Serenoil bottle, drawn as composable SVG/CSS layers so we can
// animate the cap, pipette, and oil level independently of the bottle body.

// The full bottle is sized inside a 320×640 box. Pieces are positioned
// absolutely inside a wrapper so callers can transform them individually.
//
// Pieces:
//   <BottleBody />     — amber glass + label
//   <BottleCap />      — black ribbed cap + bulb (the "lid")
//   <Pipette />        — glass tube hidden under the cap
//
// Callers pass transforms via wrapper divs.

const AMBER_DARK = '#3a1a09';
const AMBER_MID  = '#6b2e10';
const AMBER_HI   = '#a85a26';
const AMBER_GLOW = '#d98b4a';
const LABEL_BG   = '#e8dec6';
const LABEL_INK  = '#1f3a2e';
const CAP_DARK   = '#0d0d0d';
const CAP_MID    = '#1f1f1f';
const CAP_HI     = '#3a3a3a';

// — Bottle body (glass + label). No cap. —
function BottleBody({ oilLevel = 0.78 }) {
  // oilLevel: 0..1 fraction of bottle filled (visual only)
  return (
    <svg width="320" height="640" viewBox="0 0 320 640" style={{ display: 'block', overflow: 'visible' }}>
      <defs>
        {/* Glass body gradient: dark edges, lighter center band */}
        <linearGradient id="glassBody" x1="0" x2="1" y1="0" y2="0">
          <stop offset="0"    stopColor="#1f0a03"/>
          <stop offset="0.18" stopColor={AMBER_DARK}/>
          <stop offset="0.42" stopColor={AMBER_MID}/>
          <stop offset="0.58" stopColor={AMBER_HI}/>
          <stop offset="0.82" stopColor={AMBER_MID}/>
          <stop offset="1"    stopColor="#1f0a03"/>
        </linearGradient>

        {/* Vertical highlight stripe — fakes a glass reflection */}
        <linearGradient id="glassShine" x1="0" x2="0" y1="0" y2="1">
          <stop offset="0"    stopColor="#fff" stopOpacity="0"/>
          <stop offset="0.15" stopColor="#fff" stopOpacity="0.25"/>
          <stop offset="0.55" stopColor="#fff" stopOpacity="0.08"/>
          <stop offset="1"    stopColor="#fff" stopOpacity="0"/>
        </linearGradient>

        {/* Bottom shadow inside the bottle */}
        <radialGradient id="innerShadow" cx="0.5" cy="1" r="0.7">
          <stop offset="0" stopColor="#000" stopOpacity="0.55"/>
          <stop offset="1" stopColor="#000" stopOpacity="0"/>
        </radialGradient>

        {/* Bottle silhouette as a clip — for inner highlights/oil */}
        <clipPath id="bodyClip">
          <path d="M 90 130 L 90 175 Q 70 205 70 245 L 70 580 Q 70 615 105 615 L 215 615 Q 250 615 250 580 L 250 245 Q 250 205 230 175 L 230 130 Z"/>
        </clipPath>

        {/* Label paper texture using subtle noise via filter */}
        <filter id="paperGrain" x="0" y="0" width="1" height="1">
          <feTurbulence type="fractalNoise" baseFrequency="0.9" numOctaves="2" result="n"/>
          <feColorMatrix in="n" type="matrix" values="0 0 0 0 0.6  0 0 0 0 0.55  0 0 0 0 0.42  0 0 0 0.18 0"/>
          <feComposite in2="SourceGraphic" operator="in"/>
        </filter>
      </defs>

      {/* — Bottle main silhouette — */}
      <path
        d="M 90 130
           L 90 175
           Q 70 205 70 245
           L 70 580
           Q 70 615 105 615
           L 215 615
           Q 250 615 250 580
           L 250 245
           Q 250 205 230 175
           L 230 130 Z"
        fill="url(#glassBody)"
      />

      {/* Inner shadow at base */}
      <rect x="70" y="400" width="180" height="215" fill="url(#innerShadow)" clipPath="url(#bodyClip)"/>

      {/* Glass shine on left */}
      <rect x="84" y="180" width="22" height="420" fill="url(#glassShine)" clipPath="url(#bodyClip)" opacity="0.9"/>
      {/* Subtle right-edge highlight */}
      <rect x="218" y="200" width="10" height="380" fill="url(#glassShine)" clipPath="url(#bodyClip)" opacity="0.4"/>

      {/* Oil level inside — only visible if you remove the label clip; we keep it implied behind the glass color */}

      {/* Neck rim */}
      <rect x="86" y="125" width="148" height="8" rx="2" fill="#1f0a03"/>
      <rect x="86" y="125" width="148" height="2" fill="#a85a26" opacity="0.5"/>

      {/* — Label — */}
      <g>
        {/* Paper */}
        <rect x="60" y="280" width="200" height="270" rx="4" fill={LABEL_BG}/>
        {/* Curved highlight to simulate cylinder wrap */}
        <rect x="60" y="280" width="200" height="270" rx="4"
              fill="url(#labelShine)"/>
        <defs>
          <linearGradient id="labelShine" x1="0" x2="1" y1="0" y2="0">
            <stop offset="0"    stopColor="#000" stopOpacity="0.18"/>
            <stop offset="0.15" stopColor="#000" stopOpacity="0"/>
            <stop offset="0.85" stopColor="#000" stopOpacity="0"/>
            <stop offset="1"    stopColor="#000" stopOpacity="0.18"/>
          </linearGradient>
        </defs>

        {/* Leaf+drop icon */}
        <g transform="translate(160 312)">
          {/* drop outline */}
          <path d="M 0 -16 C -10 -4, -14 4, -14 12 C -14 22, -6 28, 0 28 C 6 28, 14 22, 14 12 C 14 4, 10 -4, 0 -16 Z"
                fill="none" stroke={LABEL_INK} strokeWidth="2.2"/>
          {/* leaf inside drop */}
          <path d="M 0 22 C 0 12, -8 6, -8 -2 M 0 22 C 0 12, 8 6, 8 -2"
                fill="none" stroke={LABEL_INK} strokeWidth="1.8" strokeLinecap="round"/>
          <path d="M -8 -2 C -4 -4, -1 -2, 0 4 C 1 -2, 4 -4, 8 -2 C 6 6, 2 10, 0 12 C -2 10, -6 6, -8 -2 Z"
                fill={LABEL_INK} opacity="0.0"/>
          <path d="M -8 -2 C -4 0, -2 4, 0 10 M 8 -2 C 4 0, 2 4, 0 10"
                fill="none" stroke={LABEL_INK} strokeWidth="1.4" strokeLinecap="round"/>
        </g>

        {/* "Serenoil" wordmark */}
        <text x="160" y="395"
              textAnchor="middle"
              fontFamily="'Cormorant Garamond', 'Playfair Display', Georgia, serif"
              fontWeight="600"
              fontSize="40"
              letterSpacing="-0.5"
              fill={LABEL_INK}>
          Serenoil
        </text>

        {/* "100% PURE / JOJOBA OIL" */}
        <text x="160" y="430"
              textAnchor="middle"
              fontFamily="'Inter', system-ui, sans-serif"
              fontWeight="700"
              fontSize="14"
              letterSpacing="1.5"
              fill={LABEL_INK}>
          100% PURE
        </text>
        <text x="160" y="450"
              textAnchor="middle"
              fontFamily="'Inter', system-ui, sans-serif"
              fontWeight="700"
              fontSize="14"
              letterSpacing="1.5"
              fill={LABEL_INK}>
          JOJOBA OIL
        </text>

        {/* divider line */}
        <line x1="92" y1="468" x2="228" y2="468" stroke={LABEL_INK} strokeWidth="0.8" opacity="0.5"/>

        {/* meta */}
        <text x="160" y="486"
              textAnchor="middle"
              fontFamily="'Inter', system-ui, sans-serif"
              fontWeight="500"
              fontSize="10"
              letterSpacing="0.5"
              fill={LABEL_INK}>
          Cold Pressed · Organic · Natural
        </text>
        <text x="160" y="514"
              textAnchor="middle"
              fontFamily="'Cormorant Garamond', Georgia, serif"
              fontStyle="italic"
              fontSize="14"
              fill={LABEL_INK}>
          For Hair &amp; Skin Care
        </text>
        <text x="160" y="538"
              textAnchor="middle"
              fontFamily="'Inter', system-ui, sans-serif"
              fontSize="10"
              letterSpacing="0.5"
              fill={LABEL_INK}
              opacity="0.7">
          Net: 50 ml
        </text>
      </g>

      {/* Bottom shadow on bottle */}
      <ellipse cx="160" cy="615" rx="80" ry="6" fill="#000" opacity="0.4"/>
    </svg>
  );
}

// — Cap (black ribbed grip + rubber bulb on top). Sits at the top of the bottle. —
// Anchor point: bottom-center of cap = (160, 130) in bottle coords.
function BottleCap() {
  return (
    <svg width="320" height="200" viewBox="0 0 320 200" style={{ display: 'block', overflow: 'visible' }}>
      <defs>
        <linearGradient id="capGrad" x1="0" x2="1" y1="0" y2="0">
          <stop offset="0"    stopColor="#000"/>
          <stop offset="0.15" stopColor={CAP_DARK}/>
          <stop offset="0.5"  stopColor={CAP_HI}/>
          <stop offset="0.85" stopColor={CAP_DARK}/>
          <stop offset="1"    stopColor="#000"/>
        </linearGradient>
        <linearGradient id="bulbGrad" x1="0" x2="1" y1="0" y2="0">
          <stop offset="0"    stopColor="#0a0a0a"/>
          <stop offset="0.5"  stopColor="#2a2a2a"/>
          <stop offset="1"    stopColor="#0a0a0a"/>
        </linearGradient>
      </defs>

      {/* Cap base ring */}
      <rect x="84" y="110" width="152" height="80" rx="4" fill="url(#capGrad)"/>

      {/* Ribs */}
      {Array.from({ length: 18 }, (_, i) => (
        <rect key={i} x={88 + i * 8} y="116" width="2" height="68" fill="#000" opacity="0.55"/>
      ))}
      {/* Top + bottom edge highlights */}
      <rect x="84" y="110" width="152" height="3" fill="#fff" opacity="0.06"/>
      <rect x="84" y="186" width="152" height="3" fill="#000" opacity="0.5"/>

      {/* Cap shoulder above ribs */}
      <rect x="100" y="92" width="120" height="22" rx="3" fill="url(#capGrad)"/>
      <rect x="100" y="92" width="120" height="2" fill="#fff" opacity="0.08"/>

      {/* Rubber bulb */}
      <ellipse cx="160" cy="48" rx="34" ry="50" fill="url(#bulbGrad)"/>
      <ellipse cx="148" cy="38" rx="6" ry="14" fill="#fff" opacity="0.10"/>
      {/* bulb base flange */}
      <rect x="120" y="86" width="80" height="10" rx="3" fill="#0a0a0a"/>
    </svg>
  );
}

// — Pipette (clear glass tube with amber oil inside). Hidden under the cap.
// Tube is 6px wide, ~140 long, with rounded tip. Top is the threaded collar.
function Pipette({ oilLevel = 0.7 }) {
  return (
    <svg width="60" height="220" viewBox="0 0 60 220" style={{ display: 'block', overflow: 'visible' }}>
      <defs>
        <linearGradient id="tubeGlass" x1="0" x2="1" y1="0" y2="0">
          <stop offset="0"   stopColor="#fff" stopOpacity="0.15"/>
          <stop offset="0.5" stopColor="#fff" stopOpacity="0.45"/>
          <stop offset="1"   stopColor="#fff" stopOpacity="0.10"/>
        </linearGradient>
        <linearGradient id="oilInTube" x1="0" x2="1" y1="0" y2="0">
          <stop offset="0"   stopColor="#7a3a12"/>
          <stop offset="0.5" stopColor={AMBER_GLOW}/>
          <stop offset="1"   stopColor="#7a3a12"/>
        </linearGradient>
      </defs>

      {/* Top collar (black, threads into cap) */}
      <rect x="14" y="0" width="32" height="18" fill="#0a0a0a"/>
      <rect x="14" y="0" width="32" height="2" fill="#3a3a3a"/>

      {/* Tube glass outline */}
      <rect x="25" y="18" width="10" height="180" rx="4" fill="rgba(255,255,255,0.08)" stroke="rgba(255,255,255,0.35)" strokeWidth="0.6"/>

      {/* Oil column inside */}
      {(() => {
        const tubeTop = 22;
        const tubeBottom = 196;
        const span = tubeBottom - tubeTop;
        const filled = span * oilLevel;
        const oilTop = tubeBottom - filled;
        return (
          <rect x="26.5" y={oilTop} width="7" height={filled} fill="url(#oilInTube)"/>
        );
      })()}

      {/* Rounded tip */}
      <path d="M 25 196 Q 30 210, 35 196 Z" fill={AMBER_GLOW}/>

      {/* Glass highlight stripe */}
      <rect x="26.5" y="22" width="1.5" height="174" fill="#fff" opacity="0.35"/>
    </svg>
  );
}

Object.assign(window, { BottleBody, BottleCap, Pipette });
