// ABOUTME: Four painterly Velaris-inspired SVG scenes, each viewBox-scaled to any container.
// ABOUTME: Original geometry — gradients, mountain silhouettes, scattered stars, no fan-art reference.

const sceneCommon = {
  // Deterministic star scatter: same seed always yields the same starfield.
  scatterStars(seed, count, w, h, opts = {}) {
    const stars = [];
    let s = seed;
    const rand = () => {
      s = (s * 9301 + 49297) % 233280;
      return s / 233280;
    };
    for (let i = 0; i < count; i++) {
      stars.push({
        x: rand() * w,
        y: rand() * (opts.maxY || h * 0.55),
        r: rand() * 1.4 + 0.3,
        o: rand() * 0.7 + 0.25,
      });
    }
    return stars;
  },
};

// 1. Starfall Peaks — towering mountains under a meteor sky.
function SceneStarfallPeaks({ id = "sp" }) {
  const stars = sceneCommon.scatterStars(7, 140, 1264, 1680, { maxY: 900 });
  return (
    <svg viewBox="0 0 1264 1680" preserveAspectRatio="xMidYMid slice" xmlns="http://www.w3.org/2000/svg" style={{ width: "100%", height: "100%", display: "block" }}>
      <defs>
        <linearGradient id={`${id}-sky`} x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%" stopColor="#1f1850" />
          <stop offset="35%" stopColor="#3a2778" />
          <stop offset="65%" stopColor="#6e4aac" />
          <stop offset="100%" stopColor="#a47bd0" />
        </linearGradient>
        <linearGradient id={`${id}-mtn-far`} x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%" stopColor="#8a6fb8" />
          <stop offset="100%" stopColor="#3a2768" />
        </linearGradient>
        <linearGradient id={`${id}-mtn-mid`} x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%" stopColor="#c9b3e8" />
          <stop offset="60%" stopColor="#5a3f8a" />
          <stop offset="100%" stopColor="#1f1340" />
        </linearGradient>
        <linearGradient id={`${id}-mtn-near`} x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%" stopColor="#2a1a4a" />
          <stop offset="100%" stopColor="#0d0820" />
        </linearGradient>
        <radialGradient id={`${id}-glow`} cx="0.5" cy="0.35" r="0.4">
          <stop offset="0%" stopColor="#fff5d6" stopOpacity="0.4" />
          <stop offset="100%" stopColor="#fff5d6" stopOpacity="0" />
        </radialGradient>
        <radialGradient id={`${id}-bigstar`} cx="0.5" cy="0.5" r="0.5">
          <stop offset="0%" stopColor="#fffbe6" stopOpacity="1" />
          <stop offset="20%" stopColor="#fff0c0" stopOpacity="0.9" />
          <stop offset="60%" stopColor="#d9b3ff" stopOpacity="0.3" />
          <stop offset="100%" stopColor="#d9b3ff" stopOpacity="0" />
        </radialGradient>
      </defs>

      <rect width="1264" height="1680" fill={`url(#${id}-sky)`} />
      <rect width="1264" height="1680" fill={`url(#${id}-glow)`} />

      {stars.map((st, i) => (
        <circle key={i} cx={st.x} cy={st.y} r={st.r} fill="#fff8e0" opacity={st.o} />
      ))}

      {/* Three guiding stars */}
      <circle cx="500" cy="280" r="60" fill={`url(#${id}-bigstar)`} />
      <circle cx="500" cy="280" r="3" fill="#fffce8" />
      <circle cx="640" cy="220" r="80" fill={`url(#${id}-bigstar)`} />
      <circle cx="640" cy="220" r="4" fill="#fffce8" />
      <circle cx="780" cy="280" r="55" fill={`url(#${id}-bigstar)`} />
      <circle cx="780" cy="280" r="3" fill="#fffce8" />

      {/* Meteor */}
      <line x1="180" y1="120" x2="280" y2="200" stroke="#fff8e0" strokeWidth="1.5" opacity="0.8" />
      <circle cx="280" cy="200" r="2.5" fill="#fff8e0" />

      {/* Far mountains */}
      <path d="M0,1100 L120,950 L240,1020 L360,880 L500,960 L640,820 L780,920 L900,880 L1040,1000 L1160,940 L1264,1020 L1264,1680 L0,1680 Z" fill={`url(#${id}-mtn-far)`} opacity="0.7" />

      {/* Mid mountains — central peak */}
      <path d="M0,1280 L160,1100 L320,1180 L480,920 L632,640 L780,940 L920,1080 L1080,1020 L1200,1140 L1264,1100 L1264,1680 L0,1680 Z" fill={`url(#${id}-mtn-mid)`} />

      {/* Snow caps */}
      <path d="M580,720 L632,640 L684,720 L660,740 L640,720 L620,740 Z" fill="#f5e9ff" opacity="0.85" />
      <path d="M460,960 L480,920 L510,950 L490,970 Z" fill="#f5e9ff" opacity="0.7" />
      <path d="M760,970 L780,940 L810,980 L790,990 Z" fill="#f5e9ff" opacity="0.6" />

      {/* Near silhouette */}
      <path d="M0,1480 L100,1380 L240,1440 L400,1340 L560,1420 L720,1360 L880,1430 L1040,1370 L1200,1440 L1264,1410 L1264,1680 L0,1680 Z" fill={`url(#${id}-mtn-near)`} />

      {/* City embers in the foothills */}
      {[420, 460, 500, 540, 580, 620, 660, 700, 740, 780, 820, 860].map((x, i) => (
        <circle key={i} cx={x} cy={1500 + (i % 3) * 8} r="1.2" fill="#ffd99e" opacity={0.6 + (i % 3) * 0.12} />
      ))}
    </svg>
  );
}

// 2. Sidra River — luminous river winding through a city of starlight.
function SceneSidraRiver({ id = "sr" }) {
  const stars = sceneCommon.scatterStars(13, 100, 1264, 1680, { maxY: 800 });
  return (
    <svg viewBox="0 0 1264 1680" preserveAspectRatio="xMidYMid slice" xmlns="http://www.w3.org/2000/svg" style={{ width: "100%", height: "100%", display: "block" }}>
      <defs>
        <linearGradient id={`${id}-sky`} x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%" stopColor="#2c1f64" />
          <stop offset="50%" stopColor="#604098" />
          <stop offset="100%" stopColor="#b08bd4" />
        </linearGradient>
        <linearGradient id={`${id}-river`} x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%" stopColor="#e8d4ff" />
          <stop offset="50%" stopColor="#b78bff" />
          <stop offset="100%" stopColor="#5a3f99" />
        </linearGradient>
        <radialGradient id={`${id}-moon`} cx="0.5" cy="0.5" r="0.5">
          <stop offset="0%" stopColor="#fff8e0" />
          <stop offset="60%" stopColor="#fce5b8" stopOpacity="0.7" />
          <stop offset="100%" stopColor="#fce5b8" stopOpacity="0" />
        </radialGradient>
        <linearGradient id={`${id}-hill`} x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%" stopColor="#2a1a55" />
          <stop offset="100%" stopColor="#0e0828" />
        </linearGradient>
      </defs>

      <rect width="1264" height="1680" fill={`url(#${id}-sky)`} />

      {/* Moon */}
      <circle cx="900" cy="380" r="180" fill={`url(#${id}-moon)`} />
      <circle cx="900" cy="380" r="80" fill="#fff8e0" opacity="0.95" />

      {stars.map((st, i) => (
        <circle key={i} cx={st.x} cy={st.y} r={st.r} fill="#fff8e0" opacity={st.o} />
      ))}

      {/* Hills */}
      <path d="M0,900 L200,820 L400,880 L600,780 L800,860 L1000,800 L1264,880 L1264,1680 L0,1680 Z" fill={`url(#${id}-hill)`} opacity="0.85" />

      {/* River — sweeping S-curve */}
      <path d="M-50,1680 C200,1500 100,1300 350,1200 C600,1100 700,1000 900,920 L1100,840 L1264,820 L1264,1680 Z" fill={`url(#${id}-river)`} opacity="0.95" />
      <path d="M-50,1680 C200,1500 100,1300 350,1200 C600,1100 700,1000 900,920 L1100,840 L1264,820" stroke="#fff8e0" strokeWidth="2" fill="none" opacity="0.6" />

      {/* River shimmer */}
      {[
        { x: 200, y: 1450, r: 8 }, { x: 280, y: 1380, r: 6 }, { x: 350, y: 1280, r: 10 },
        { x: 480, y: 1200, r: 7 }, { x: 600, y: 1140, r: 9 }, { x: 720, y: 1060, r: 6 },
        { x: 850, y: 980, r: 8 }, { x: 1000, y: 900, r: 7 },
      ].map((p, i) => (
        <circle key={i} cx={p.x} cy={p.y} r={p.r} fill="#fff8e0" opacity="0.5" />
      ))}

      {/* Spires */}
      {[150, 280, 380, 700, 820, 1080].map((x, i) => {
        const h = 120 + (i * 37 % 80);
        const baseY = 880 - (i * 23 % 60);
        return (
          <g key={i}>
            <path d={`M${x - 14},${baseY} L${x},${baseY - h} L${x + 14},${baseY} Z`} fill="#1a0e3a" />
            <circle cx={x} cy={baseY - h} r="2" fill="#ffd99e" opacity="0.8" />
            <rect x={x - 3} y={baseY - h * 0.6} width="2" height="3" fill="#ffd99e" opacity="0.7" />
          </g>
        );
      })}
    </svg>
  );
}

// 3. Velaris Bay — harbor view with cliffs and a constellation.
function SceneVelarisBay({ id = "vb" }) {
  const stars = sceneCommon.scatterStars(23, 130, 1264, 1680, { maxY: 1000 });
  return (
    <svg viewBox="0 0 1264 1680" preserveAspectRatio="xMidYMid slice" xmlns="http://www.w3.org/2000/svg" style={{ width: "100%", height: "100%", display: "block" }}>
      <defs>
        <linearGradient id={`${id}-sky`} x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%" stopColor="#241858" />
          <stop offset="40%" stopColor="#4d2f8d" />
          <stop offset="80%" stopColor="#8a5cb5" />
          <stop offset="100%" stopColor="#c298d8" />
        </linearGradient>
        <linearGradient id={`${id}-water`} x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%" stopColor="#5e3a8a" />
          <stop offset="100%" stopColor="#150a30" />
        </linearGradient>
        <linearGradient id={`${id}-cliff`} x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%" stopColor="#3a2560" />
          <stop offset="100%" stopColor="#0a0520" />
        </linearGradient>
      </defs>

      <rect width="1264" height="1680" fill={`url(#${id}-sky)`} />

      {/* Aurora sweep */}
      <ellipse cx="600" cy="700" rx="900" ry="200" fill="#a87dd9" opacity="0.18" />
      <ellipse cx="700" cy="850" rx="700" ry="120" fill="#e0b3ff" opacity="0.12" />

      {stars.map((st, i) => (
        <circle key={i} cx={st.x} cy={st.y} r={st.r} fill="#fff8e0" opacity={st.o} />
      ))}

      {/* Constellation — connect the dots */}
      {[[280, 240], [380, 200], [460, 280], [540, 220], [640, 320], [740, 260]].map((p, i, arr) => (
        <g key={i}>
          {i > 0 && <line x1={arr[i - 1][0]} y1={arr[i - 1][1]} x2={p[0]} y2={p[1]} stroke="#fff8e0" strokeWidth="0.6" opacity="0.4" />}
          <circle cx={p[0]} cy={p[1]} r="3" fill="#fffbe0" />
          <circle cx={p[0]} cy={p[1]} r="8" fill="#fffbe0" opacity="0.25" />
        </g>
      ))}

      {/* Water */}
      <rect x="0" y="1100" width="1264" height="580" fill={`url(#${id}-water)`} />

      {/* Moon-reflection ripples */}
      {[1140, 1180, 1220, 1260, 1300, 1340, 1380, 1420, 1460, 1500, 1540, 1580].map((y, i) => (
        <ellipse key={i} cx={620 + (i % 2 === 0 ? -8 : 8)} cy={y} rx={70 - i * 3} ry="2" fill="#fff8e0" opacity={0.5 - i * 0.035} />
      ))}

      {/* Left cliff with city */}
      <path d="M0,1100 L0,700 L80,720 L140,680 L200,740 L260,700 L320,780 L380,800 L420,1100 Z" fill={`url(#${id}-cliff)`} />
      {/* Right cliff */}
      <path d="M1264,1100 L1264,750 L1180,760 L1120,720 L1060,790 L1000,760 L940,820 L880,840 L860,1100 Z" fill={`url(#${id}-cliff)`} />

      {/* Embers in cliffs */}
      {[
        [60, 880], [120, 920], [180, 880], [240, 940], [300, 900], [340, 970],
        [1200, 900], [1140, 940], [1080, 900], [1020, 960], [960, 920], [920, 980],
      ].map(([x, y], i) => (
        <circle key={i} cx={x} cy={y} r="1.6" fill="#ffd99e" opacity="0.85" />
      ))}

      {/* Boats */}
      <g opacity="0.8">
        <path d="M540,1280 L580,1280 L575,1295 L545,1295 Z" fill="#0a0520" />
        <line x1="560" y1="1280" x2="560" y2="1255" stroke="#0a0520" strokeWidth="1" />
        <circle cx="560" cy="1265" r="1.5" fill="#ffd99e" />
      </g>
      <g opacity="0.7">
        <path d="M780,1340 L820,1340 L815,1355 L785,1355 Z" fill="#0a0520" />
        <line x1="800" y1="1340" x2="800" y2="1318" stroke="#0a0520" strokeWidth="1" />
        <circle cx="800" cy="1325" r="1.3" fill="#ffd99e" />
      </g>
    </svg>
  );
}

// 4. Constellation Garden — overhead view of a celestial rose in nebulae.
function SceneConstellationGarden({ id = "cg" }) {
  const stars = sceneCommon.scatterStars(31, 220, 1264, 1680, { maxY: 1680 });
  return (
    <svg viewBox="0 0 1264 1680" preserveAspectRatio="xMidYMid slice" xmlns="http://www.w3.org/2000/svg" style={{ width: "100%", height: "100%", display: "block" }}>
      <defs>
        <radialGradient id={`${id}-bg`} cx="0.5" cy="0.45" r="0.75">
          <stop offset="0%" stopColor="#6a3fa0" />
          <stop offset="50%" stopColor="#3a2470" />
          <stop offset="100%" stopColor="#1c1240" />
        </radialGradient>
        <radialGradient id={`${id}-flare`} cx="0.5" cy="0.5" r="0.5">
          <stop offset="0%" stopColor="#fff5d6" stopOpacity="1" />
          <stop offset="40%" stopColor="#e0b3ff" stopOpacity="0.4" />
          <stop offset="100%" stopColor="#e0b3ff" stopOpacity="0" />
        </radialGradient>
      </defs>

      <rect width="1264" height="1680" fill={`url(#${id}-bg)`} />

      {/* Nebula clouds */}
      <ellipse cx="400" cy="500" rx="320" ry="180" fill="#a87dd9" opacity="0.18" />
      <ellipse cx="900" cy="900" rx="380" ry="200" fill="#e0a8ff" opacity="0.14" />
      <ellipse cx="630" cy="1300" rx="450" ry="220" fill="#7c5db8" opacity="0.16" />

      {stars.map((st, i) => (
        <circle key={i} cx={st.x} cy={st.y} r={st.r} fill="#fff8e0" opacity={st.o} />
      ))}

      {/* Rose-shaped constellation, centered */}
      {(() => {
        const cx = 632, cy = 840;
        const points = [];
        for (let i = 0; i < 8; i++) {
          const a = (i / 8) * Math.PI * 2;
          points.push([cx + Math.cos(a) * 200, cy + Math.sin(a) * 200]);
          points.push([cx + Math.cos(a + Math.PI / 8) * 110, cy + Math.sin(a + Math.PI / 8) * 110]);
        }
        return (
          <g>
            {points.map((p, i) => {
              const next = points[(i + 1) % points.length];
              return <line key={`l${i}`} x1={p[0]} y1={p[1]} x2={next[0]} y2={next[1]} stroke="#fff8e0" strokeWidth="0.6" opacity="0.3" />;
            })}
            {points.map((p, i) => (
              <g key={`p${i}`}>
                <circle cx={p[0]} cy={p[1]} r="14" fill={`url(#${id}-flare)`} />
                <circle cx={p[0]} cy={p[1]} r={i % 2 === 0 ? 3 : 2} fill="#fffbe0" />
              </g>
            ))}
            <circle cx={cx} cy={cy} r="40" fill={`url(#${id}-flare)`} />
            <circle cx={cx} cy={cy} r="5" fill="#fffbe0" />
          </g>
        );
      })()}

      {/* Ambient flares */}
      <circle cx="240" cy="280" r="40" fill={`url(#${id}-flare)`} />
      <circle cx="1040" cy="380" r="50" fill={`url(#${id}-flare)`} />
      <circle cx="180" cy="1320" r="45" fill={`url(#${id}-flare)`} />
      <circle cx="1080" cy="1440" r="35" fill={`url(#${id}-flare)`} />
    </svg>
  );
}

// Scene registry. SVG scenes render a React Component; image scenes render
// a plain <img> covering the container. Existing IDs are preserved so saved
// drafts and library entries continue to resolve after the rename.
const SCENES_DEF = [
  { id: "starfall",  kind: "svg",   name: "Starfall Peaks",    Component: SceneStarfallPeaks,    blurb: "Three guiding stars over a moonlit range" },
  { id: "sidra",     kind: "image", name: "City of Starlight", src: "assets/velaris.png",        blurb: "A painted view of Velaris by night" },
  { id: "bay",       kind: "image", name: "Three Sisters",     src: "assets/velaris_2.png",      blurb: "Three guiding stars over the central peak" },
  { id: "garden",    kind: "image", name: "Starlit Vale",      src: "assets/Velaris_v0.png",     blurb: "A small valley under the northern stars" },
  { id: "feyre",     kind: "image", name: "The Painter",       src: "assets/feyre.png",          blurb: "Feyre at her easel beneath the moon" },
  { id: "rhysfeyre", kind: "image", name: "On the Balcony",    src: "assets/rhys-and-feyre.png", blurb: "Two silhouettes overlooking the City" },
];

function getSceneDef(id) {
  return SCENES_DEF.find(s => s.id === id) || SCENES_DEF[0];
}

// Unified renderer: chooses the right representation based on scene kind.
function SceneRenderer({ sceneId, id }) {
  const def = getSceneDef(sceneId);
  if (def.kind === "image") {
    return (
      <img
        src={def.src}
        alt=""
        draggable={false}
        style={{
          position: "absolute", inset: 0,
          width: "100%", height: "100%",
          objectFit: "cover", display: "block",
          pointerEvents: "none",
        }}
      />
    );
  }
  const C = def.Component;
  return <C id={id} />;
}

Object.assign(window, {
  SceneStarfallPeaks, SceneSidraRiver, SceneVelarisBay, SceneConstellationGarden,
  SCENES_DEF, getSceneDef, SceneRenderer,
});
