// FloatingIsland — the villa on a rock, painterly, drawn in CSS/SVG.
// Includes slow vertical bob. Sheep are separate components.

function FloatingIsland({ x = 960, y = 540, scale = 1, bob = true }) {
  const time = useTime();
  const by = bob ? Math.sin(time * 0.6) * 8 : 0;

  return (
    <div style={{
      position: 'absolute',
      left: x, top: y + by,
      transform: `translate(-50%, -50%) scale(${scale})`,
      transformOrigin: 'center',
      willChange: 'transform',
    }}>
      {/* Rock base */}
      <svg width="820" height="620" viewBox="0 0 820 620" style={{ overflow: 'visible' }}>
        <defs>
          <linearGradient id="rock" x1="0" y1="0" x2="0" y2="1">
            <stop offset="0" stopColor="#7a6b5c"/>
            <stop offset="0.4" stopColor="#5e5244"/>
            <stop offset="1" stopColor="#3a3128"/>
          </linearGradient>
          <linearGradient id="grass" x1="0" y1="0" x2="0" y2="1">
            <stop offset="0" stopColor="#a8b66a"/>
            <stop offset="1" stopColor="#6b7a3e"/>
          </linearGradient>
          <linearGradient id="villaBody" x1="0" y1="0" x2="0" y2="1">
            <stop offset="0" stopColor="#c07a5e"/>
            <stop offset="0.5" stopColor="#8e5138"/>
            <stop offset="1" stopColor="#5a3424"/>
          </linearGradient>
          <linearGradient id="roof" x1="0" y1="0" x2="0" y2="1">
            <stop offset="0" stopColor="#6a5748"/>
            <stop offset="1" stopColor="#3c2f24"/>
          </linearGradient>
          <radialGradient id="windowGlow" cx="0.5" cy="0.3" r="0.7">
            <stop offset="0" stopColor="#ffe6a8"/>
            <stop offset="1" stopColor="#d4a766"/>
          </radialGradient>
          <filter id="soft">
            <feGaussianBlur stdDeviation="0.4"/>
          </filter>
        </defs>

        {/* Rock underside — jagged, tapering to a point */}
        <path d="M 140 380 L 90 360 L 70 400 L 120 470 L 180 540 L 260 580 L 360 595 L 460 580 L 560 560 L 640 520 L 700 470 L 720 420 L 680 390 L 600 380 Z"
          fill="url(#rock)" filter="url(#soft)"/>

        {/* Secondary rock chunk for depth */}
        <path d="M 300 560 L 280 590 L 360 610 L 400 590 L 380 570 Z"
          fill="#2a2118" opacity="0.7"/>

        {/* Grass top surface */}
        <path d="M 100 380 Q 200 350 360 360 Q 540 350 700 380 L 700 410 Q 520 430 360 425 Q 200 425 100 410 Z"
          fill="url(#grass)"/>

        {/* Grass tufts */}
        {[150, 220, 290, 380, 460, 540, 620].map((gx, i) => (
          <g key={i}>
            <path d={`M ${gx} 380 l -4 -10 l 4 2 l 3 -8 l 3 10 l 5 -3 l -2 9 z`} fill="#8fa054" opacity="0.85"/>
          </g>
        ))}

        {/* Villa — main body */}
        <g transform="translate(360 130)">
          {/* Back tower */}
          <rect x="130" y="30" width="90" height="230" fill="url(#villaBody)"/>
          <polygon points="125,30 175,-20 225,30" fill="url(#roof)"/>

          {/* Main body */}
          <rect x="30" y="80" width="180" height="180" fill="url(#villaBody)"/>
          {/* Roof - gabled */}
          <polygon points="20,80 120,10 220,80" fill="url(#roof)"/>
          {/* Ornate gable */}
          <polygon points="90,50 120,20 150,50 150,80 90,80" fill="#5d4637"/>
          <circle cx="120" cy="45" r="6" fill="url(#windowGlow)"/>

          {/* Windows — arched, glowing */}
          <rect x="50" y="110" width="28" height="48" rx="14" fill="url(#windowGlow)"/>
          <rect x="95" y="110" width="28" height="48" rx="14" fill="url(#windowGlow)"/>
          <rect x="140" y="110" width="28" height="48" rx="14" fill="url(#windowGlow)"/>

          <rect x="50" y="180" width="28" height="48" rx="14" fill="url(#windowGlow)"/>
          <rect x="95" y="180" width="28" height="48" rx="14" fill="url(#windowGlow)"/>
          <rect x="140" y="180" width="28" height="48" rx="14" fill="url(#windowGlow)"/>

          {/* Back tower windows */}
          <rect x="150" y="60" width="20" height="38" rx="10" fill="url(#windowGlow)"/>
          <rect x="180" y="60" width="20" height="38" rx="10" fill="url(#windowGlow)"/>
          <rect x="150" y="120" width="20" height="38" rx="10" fill="url(#windowGlow)"/>
          <rect x="180" y="120" width="20" height="38" rx="10" fill="url(#windowGlow)"/>

          {/* Iron balcony */}
          <rect x="28" y="170" width="184" height="3" fill="#2a2118"/>
          <g stroke="#2a2118" strokeWidth="1.5" fill="none">
            {Array.from({ length: 18 }).map((_, i) => (
              <line key={i} x1={32 + i * 10} y1="173" x2={32 + i * 10} y2="195"/>
            ))}
          </g>
          <rect x="28" y="193" width="184" height="2" fill="#2a2118"/>

          {/* Chimneys */}
          <rect x="180" y="-5" width="14" height="30" fill="#5d4637"/>
          <rect x="50" y="20" width="12" height="25" fill="#5d4637"/>

          {/* Spire */}
          <polygon points="170,-20 175,-50 180,-20" fill="#2a2118"/>
          <line x1="175" y1="-50" x2="175" y2="-62" stroke="#2a2118" strokeWidth="1"/>
          <polygon points="172,-62 178,-62 175,-70" fill="#2a2118"/>
        </g>
      </svg>
    </div>
  );
}

// A single purple sheep — painterly. Props: x, y, size, facing (1 or -1), bob.
function Sheep({ x = 100, y = 300, size = 1, facing = 1, bobPhase = 0, tone = '#b9a4d4' }) {
  const time = useTime();
  const bob = Math.sin(time * 1.2 + bobPhase) * 1.5;

  return (
    <div style={{
      position: 'absolute',
      left: x, top: y + bob,
      transform: `translate(-50%, -50%) scale(${size * facing}, ${size})`,
      transformOrigin: 'center',
    }}>
      <svg width="120" height="90" viewBox="0 0 120 90" style={{ overflow: 'visible' }}>
        <defs>
          <radialGradient id={`wool-${bobPhase}`} cx="0.5" cy="0.4" r="0.6">
            <stop offset="0" stopColor="#d6c3ea"/>
            <stop offset="0.6" stopColor={tone}/>
            <stop offset="1" stopColor="#7d6399"/>
          </radialGradient>
        </defs>
        {/* Body */}
        <ellipse cx="58" cy="52" rx="40" ry="26" fill={`url(#wool-${bobPhase})`}/>
        {/* Puffy wool bumps */}
        <circle cx="32" cy="42" r="12" fill={tone}/>
        <circle cx="48" cy="34" r="13" fill={tone}/>
        <circle cx="68" cy="34" r="13" fill={tone}/>
        <circle cx="86" cy="42" r="12" fill={tone}/>
        <circle cx="90" cy="54" r="11" fill={tone}/>
        <circle cx="28" cy="54" r="11" fill={tone}/>
        {/* Head */}
        <ellipse cx="100" cy="46" rx="14" ry="12" fill="#8b7aa1"/>
        {/* Ear */}
        <ellipse cx="94" cy="36" rx="4" ry="6" fill="#6a5a82" transform="rotate(-20 94 36)"/>
        {/* Eye */}
        <circle cx="104" cy="44" r="1.8" fill="#1a1222"/>
        {/* Legs */}
        <rect x="40" y="70" width="5" height="14" fill="#3a2e4a"/>
        <rect x="54" y="72" width="5" height="14" fill="#3a2e4a"/>
        <rect x="72" y="72" width="5" height="14" fill="#3a2e4a"/>
        <rect x="86" y="70" width="5" height="14" fill="#3a2e4a"/>
      </svg>
    </div>
  );
}

Object.assign(window, { FloatingIsland, Sheep });
