/* Tefi — Services backgrounds. Real SVG art + canvas particles, swappable. */

function ServicesBackground({ kind }) {
  if (kind === 'bone')      return <SolidBg color="var(--bone)" />;
  if (kind === 'cream')     return <SolidBg color="var(--cream)" />;
  if (kind === 'ink')       return <SolidBg color="var(--ink)" />;
  if (kind === 'sage')      return <SolidBg color="var(--sage)" />;
  if (kind === 'gradient')  return <GradientBg />;
  if (kind === 'botanical') return <BotanicalBg />;
  if (kind === 'arches')    return <ArchesBg />;
  if (kind === 'pearl')     return <PearlBg />;
  if (kind === 'brushstroke') return <BrushstrokeBg />;
  if (kind === 'sparkles')  return <SparklesCanvas />;
  if (kind === 'spotlight') return <SpotlightBg />;
  return <SolidBg color="var(--bone)" />;
}

function SolidBg({ color }) {
  return <div className="absolute inset-0" style={{ background: color }} aria-hidden></div>;
}

function GradientBg() {
  return (
    <div className="absolute inset-0" aria-hidden style={{
      background: 'radial-gradient(ellipse 80% 60% at 20% 10%, color-mix(in srgb, var(--accent) 22%, var(--bone)) 0%, var(--bone) 55%), radial-gradient(ellipse 70% 50% at 90% 100%, color-mix(in srgb, var(--sage) 30%, var(--bone)) 0%, transparent 60%), var(--bone)'
    }}></div>
  );
}

/* Botanical · large tonal pressed-leaf silhouettes scattered across the section.
   Drawn slowly with proper composition — think pressed flower archive. */
function BotanicalBg() {
  return (
    <div className="absolute inset-0 overflow-hidden" aria-hidden style={{ background: 'var(--bone)' }}>
      <svg className="absolute inset-0 w-full h-full" viewBox="0 0 1600 1600" preserveAspectRatio="xMidYMid slice">
        <defs>
          <radialGradient id="vignette" cx="50%" cy="50%" r="80%">
            <stop offset="0%" stopColor="transparent" />
            <stop offset="100%" stopColor="color-mix(in srgb, var(--ink) 6%, transparent)" />
          </radialGradient>
        </defs>
        {/* very subtle leaves at low opacity, layered for depth */}
        <g opacity="0.08" style={{ color: 'var(--sage-deep)' }}>
          {/* fern frond top-left */}
          <g transform="translate(40,80) rotate(-18)">
            <path d="M0 0 Q 200 -20 380 60 Q 520 120 600 240" stroke="currentColor" strokeWidth="2" fill="none" />
            {Array.from({ length: 14 }).map((_, i) => {
              const t = i / 13;
              const x = 600 * t + 40 * Math.sin(t * 3);
              const y = 240 * t * t - 20 * (1 - t);
              const r = 8 + 22 * (1 - t);
              return (
                <ellipse key={`f1-${i}`} cx={x} cy={y - 18} rx={r * 1.6} ry={r * 0.5}
                  fill="currentColor" transform={`rotate(${-30 + i * 4} ${x} ${y - 18})`} />
              );
            })}
          </g>
          {/* eucalyptus right */}
          <g transform="translate(1300,200) rotate(28)">
            <path d="M0 0 C 60 200 80 420 40 700" stroke="currentColor" strokeWidth="2" fill="none" />
            {Array.from({ length: 16 }).map((_, i) => {
              const y = i * 44;
              const side = i % 2 === 0 ? -1 : 1;
              const x = side * (24 + (i % 4) * 4);
              return (
                <ellipse key={`e-${i}`} cx={x} cy={y} rx="34" ry="14" fill="currentColor"
                  transform={`rotate(${side * 30} ${x} ${y})`} />
              );
            })}
          </g>
          {/* trailing vine bottom-left */}
          <g transform="translate(80,1100)">
            <path d="M0 100 Q 200 0 420 80 Q 620 160 800 60" stroke="currentColor" strokeWidth="2" fill="none" />
            {Array.from({ length: 18 }).map((_, i) => {
              const t = i / 17;
              const x = 800 * t;
              const y = 100 + 80 * Math.sin(t * Math.PI * 2);
              return (
                <g key={`v-${i}`} transform={`translate(${x} ${y}) rotate(${i * 22})`}>
                  <ellipse cx="0" cy="0" rx="22" ry="9" fill="currentColor" />
                </g>
              );
            })}
          </g>
          {/* stylised flower */}
          <g transform="translate(1180,1180)">
            {Array.from({ length: 8 }).map((_, i) => (
              <ellipse key={`p-${i}`} cx="0" cy="-44" rx="24" ry="60"
                fill="currentColor" transform={`rotate(${i * 45})`} opacity="0.6" />
            ))}
            <circle cx="0" cy="0" r="14" fill="var(--accent-deep)" opacity="0.5" />
          </g>
        </g>
        <rect width="1600" height="1600" fill="url(#vignette)" />
      </svg>
      <div className="grain" style={{ opacity: 0.18 }}></div>
    </div>
  );
}

/* Arches · architectural arched panels — gallery wall feel */
function ArchesBg() {
  return (
    <div className="absolute inset-0 overflow-hidden" aria-hidden
      style={{ background: 'linear-gradient(180deg, var(--bone) 0%, color-mix(in srgb, var(--accent) 8%, var(--bone)) 100%)' }}>
      <svg className="absolute inset-0 w-full h-full" viewBox="0 0 1600 900" preserveAspectRatio="xMidYMid slice">
        <defs>
          <linearGradient id="arch-fill" x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%" stopColor="color-mix(in srgb, var(--cream) 60%, transparent)" />
            <stop offset="100%" stopColor="color-mix(in srgb, var(--cream) 0%, transparent)" />
          </linearGradient>
        </defs>
        {[0, 1, 2].map(i => (
          <path key={i}
            d={`M ${200 + i * 480} 900 L ${200 + i * 480} 320 A 180 240 0 0 1 ${560 + i * 480} 320 L ${560 + i * 480} 900 Z`}
            fill="url(#arch-fill)"
            stroke="color-mix(in srgb, var(--ink) 12%, transparent)" strokeWidth="1.5"
          />
        ))}
      </svg>
      <div className="grain" style={{ opacity: 0.22 }}></div>
    </div>
  );
}

/* Pearl · giant single sculpted nail / pearl with realistic gradient — big visual anchor */
function PearlBg() {
  return (
    <div className="absolute inset-0 overflow-hidden" aria-hidden
      style={{ background: 'radial-gradient(ellipse at 70% 50%, color-mix(in srgb, var(--accent) 20%, var(--bone)) 0%, var(--bone) 70%)' }}>
      <svg className="absolute" viewBox="0 0 600 800" style={{ right: '-100px', top: '50%', transform: 'translateY(-50%)', height: '120%', width: 'auto' }} aria-hidden>
        <defs>
          <radialGradient id="pearl-grad" cx="40%" cy="35%" r="65%">
            <stop offset="0%" stopColor="white" stopOpacity="0.55" />
            <stop offset="35%" stopColor="color-mix(in srgb, var(--accent) 35%, white)" stopOpacity="0.4" />
            <stop offset="70%" stopColor="color-mix(in srgb, var(--accent) 60%, var(--bone))" stopOpacity="0.5" />
            <stop offset="100%" stopColor="color-mix(in srgb, var(--ink) 30%, var(--accent))" stopOpacity="0.55" />
          </radialGradient>
          <radialGradient id="pearl-highlight" cx="35%" cy="25%" r="20%">
            <stop offset="0%" stopColor="white" stopOpacity="0.7" />
            <stop offset="100%" stopColor="white" stopOpacity="0" />
          </radialGradient>
        </defs>
        {/* almond nail shape */}
        <path d="M300 60 Q 480 60 510 380 Q 520 620 300 740 Q 80 620 90 380 Q 120 60 300 60 Z" fill="url(#pearl-grad)" />
        <path d="M300 60 Q 480 60 510 380 Q 520 620 300 740 Q 80 620 90 380 Q 120 60 300 60 Z" fill="url(#pearl-highlight)" />
      </svg>
      <div className="grain" style={{ opacity: 0.2 }}></div>
    </div>
  );
}

/* Brushstroke · big confident brushstrokes referencing nail polish gestures */
function BrushstrokeBg() {
  return (
    <div className="absolute inset-0 overflow-hidden" aria-hidden style={{ background: 'var(--bone)' }}>
      <svg className="absolute inset-0 w-full h-full" viewBox="0 0 1600 1000" preserveAspectRatio="xMidYMid slice">
        <defs>
          <filter id="brush-tex" x="-10%" y="-10%" width="120%" height="120%">
            <feTurbulence baseFrequency="0.9" numOctaves="2" seed="3" />
            <feDisplacementMap in="SourceGraphic" scale="6" />
          </filter>
          <linearGradient id="stroke1" x1="0" y1="0" x2="1" y2="0">
            <stop offset="0%" stopColor="color-mix(in srgb, var(--accent) 60%, transparent)" />
            <stop offset="50%" stopColor="color-mix(in srgb, var(--accent) 25%, transparent)" />
            <stop offset="100%" stopColor="transparent" />
          </linearGradient>
          <linearGradient id="stroke2" x1="0" y1="0" x2="1" y2="0">
            <stop offset="0%" stopColor="transparent" />
            <stop offset="40%" stopColor="color-mix(in srgb, var(--sage) 50%, transparent)" />
            <stop offset="100%" stopColor="color-mix(in srgb, var(--sage-deep) 30%, transparent)" />
          </linearGradient>
        </defs>
        <g opacity="0.55">
          <path d="M -50 280 C 300 220 700 320 1100 240 S 1700 280 1700 260" stroke="url(#stroke1)" strokeWidth="120" fill="none" strokeLinecap="round" filter="url(#brush-tex)" />
          <path d="M -50 720 C 400 760 800 660 1300 740 S 1700 700 1700 720" stroke="url(#stroke2)" strokeWidth="90" fill="none" strokeLinecap="round" filter="url(#brush-tex)" />
        </g>
      </svg>
      <div className="grain" style={{ opacity: 0.2 }}></div>
    </div>
  );
}

/* Spotlight · soft directional studio light + concentric arcs */
function SpotlightBg() {
  return (
    <div className="absolute inset-0 overflow-hidden" aria-hidden style={{
      background: 'radial-gradient(ellipse 60% 70% at 80% 30%, color-mix(in srgb, var(--accent) 35%, var(--bone)) 0%, var(--bone) 55%, color-mix(in srgb, var(--ink) 8%, var(--bone)) 100%)'
    }}>
      <svg className="absolute inset-0 w-full h-full" viewBox="0 0 1600 1000" preserveAspectRatio="xMidYMid slice">
        <g opacity="0.18" style={{ color: 'var(--ink)' }}>
          {[0,1,2,3,4,5].map(i => (
            <circle key={i} cx="1280" cy="300" r={200 + i * 180} fill="none" stroke="currentColor" strokeWidth="1" />
          ))}
        </g>
      </svg>
      <div className="grain" style={{ opacity: 0.18 }}></div>
    </div>
  );
}

/* Sparkles · canvas-based twinkling field with real four-point sparkles */
function SparklesCanvas() {
  const ref = React.useRef(null);

  React.useEffect(() => {
    const canvas = ref.current; if (!canvas) return;
    const ctx = canvas.getContext('2d');
    let raf, w, h;
    const stars = [];
    const sparkles = [];

    // Read accent colour from the live computed style
    const root = canvas.closest('section') || document.documentElement;
    const cs = getComputedStyle(root);
    const accent = cs.getPropertyValue('--accent').trim() || '#C97456';
    const accentDeep = cs.getPropertyValue('--accent-deep').trim() || '#9F543A';

    const resize = () => {
      const dpr = Math.min(window.devicePixelRatio || 1, 2);
      const r = canvas.getBoundingClientRect();
      w = r.width; h = r.height;
      canvas.width = w * dpr; canvas.height = h * dpr;
      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);

      stars.length = 0; sparkles.length = 0;
      const starN = Math.floor((w * h) / 8000);
      for (let i = 0; i < starN; i++) {
        stars.push({
          x: Math.random() * w,
          y: Math.random() * h,
          r: 0.6 + Math.random() * 1.4,
          tw: Math.random() * Math.PI * 2,
          twS: 0.005 + Math.random() * 0.012,
          base: 0.25 + Math.random() * 0.35,
        });
      }
      const sparkN = Math.floor((w * h) / 60000);
      for (let i = 0; i < sparkN; i++) {
        sparkles.push({
          x: Math.random() * w,
          y: Math.random() * h,
          size: 8 + Math.random() * 18,
          rot: Math.random() * Math.PI,
          tw: Math.random() * Math.PI * 2,
          twS: 0.012 + Math.random() * 0.02,
          colour: Math.random() > 0.4 ? accent : accentDeep,
        });
      }
    };

    const drawSparkle = (x, y, size, rot, alpha, colour) => {
      ctx.save();
      ctx.translate(x, y);
      ctx.rotate(rot);
      ctx.globalAlpha = alpha;
      ctx.fillStyle = colour;
      // four-point sparkle (concave diamond)
      ctx.beginPath();
      const s = size, t = size * 0.18;
      ctx.moveTo(0, -s);
      ctx.quadraticCurveTo(t, -t, s, 0);
      ctx.quadraticCurveTo(t, t, 0, s);
      ctx.quadraticCurveTo(-t, t, -s, 0);
      ctx.quadraticCurveTo(-t, -t, 0, -s);
      ctx.closePath();
      ctx.fill();
      // bright centre
      ctx.globalAlpha = alpha * 0.9;
      ctx.fillStyle = '#fff';
      ctx.beginPath(); ctx.arc(0, 0, size * 0.12, 0, Math.PI * 2); ctx.fill();
      ctx.restore();
    };

    const tick = () => {
      ctx.clearRect(0, 0, w, h);
      // small twinkling stars
      for (const s of stars) {
        s.tw += s.twS;
        const a = s.base + Math.sin(s.tw) * 0.35;
        ctx.globalAlpha = Math.max(0, a);
        ctx.fillStyle = accent;
        ctx.beginPath(); ctx.arc(s.x, s.y, s.r, 0, Math.PI * 2); ctx.fill();
      }
      // four-point sparkles
      for (const sp of sparkles) {
        sp.tw += sp.twS;
        const t = (Math.sin(sp.tw) + 1) / 2; // 0..1
        const alpha = 0.15 + t * 0.55;
        const scale = 0.6 + t * 0.6;
        drawSparkle(sp.x, sp.y, sp.size * scale, sp.rot, alpha, sp.colour);
      }
      ctx.globalAlpha = 1;
      raf = requestAnimationFrame(tick);
    };

    resize();
    tick();
    const ro = new ResizeObserver(resize);
    ro.observe(canvas);
    return () => { cancelAnimationFrame(raf); ro.disconnect(); };
  }, []);

  return (
    <div className="absolute inset-0 overflow-hidden" aria-hidden
      style={{ background: 'radial-gradient(ellipse 70% 55% at 50% 30%, color-mix(in srgb, var(--accent) 22%, var(--bone)) 0%, var(--bone) 65%), var(--bone)' }}>
      <canvas ref={ref} style={{ position: 'absolute', inset: 0, width: '100%', height: '100%' }}></canvas>
      <div className="grain" style={{ opacity: 0.18 }}></div>
    </div>
  );
}

window.ServicesBackground = ServicesBackground;
