// Generated SVG cover artwork for releases and podcast chapters.
// Six visual archetypes, deterministically chosen per `seed` (release id /
// chapter id). The goal is "looks like a real ambient / experimental record
// cover" — monochrome, abstract, textured. When `accent` is set, a faint hint
// of that hue is mixed into one layer.

// --- Seeded RNG (mulberry32) -----------------------------------------------
const hashSeed = (s) => {
  let h = 2166136261 >>> 0;
  for (let i = 0; i < s.length; i++) {
    h ^= s.charCodeAt(i);
    h = Math.imul(h, 16777619);
  }
  return h >>> 0;
};
const makeRng = (seed) => {
  let a = seed || 1;
  return () => {
    a |= 0; a = (a + 0x6D2B79F5) | 0;
    let t = Math.imul(a ^ (a >>> 15), 1 | a);
    t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
    return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
  };
};

// --- 6 visual archetypes ---------------------------------------------------
// Each returns the SVG inner markup as JSX, given (rng, accent).
// Background is always a base dark rectangle drawn by Artwork.

const Archetypes = {
  // A — Tidal rings: concentric circles, off-center, organic distortion
  tides: (rng, accent) => {
    const cx = 50 + (rng() - .5) * 40;
    const cy = 50 + (rng() - .5) * 40;
    const rings = 22 + Math.floor(rng() * 14);
    const baseFreq = (0.012 + rng() * 0.018).toFixed(4);
    const fid = 'art-tides-' + Math.floor(rng() * 1e6);
    return (
      <>
        <defs>
          <filter id={fid} x="-10%" y="-10%" width="120%" height="120%">
            <feTurbulence type="fractalNoise" baseFrequency={baseFreq} numOctaves="2" seed={Math.floor(rng() * 100)} />
            <feDisplacementMap in="SourceGraphic" scale="6" />
            <feGaussianBlur stdDeviation="0.25" />
          </filter>
        </defs>
        <g filter={`url(#${fid})`} opacity=".85">
          {Array.from({ length: rings }).map((_, i) => {
            const r = 4 + i * (90 / rings);
            const op = 0.04 + (1 - i / rings) * 0.4;
            const stroke = i % 7 === 0 && accent ? accent : '#dadada';
            return <circle key={i} cx={cx} cy={cy} r={r} fill="none" stroke={stroke} strokeWidth="0.35" opacity={op} />;
          })}
        </g>
        <radialGradient id={fid + '-v'} cx={cx + '%'} cy={cy + '%'} r="70%">
          <stop offset="0%"  stopColor="rgba(0,0,0,0)" />
          <stop offset="100%" stopColor="rgba(0,0,0,.55)" />
        </radialGradient>
        <rect x="0" y="0" width="100" height="100" fill={`url(#${fid}-v)`} />
      </>
    );
  },

  // B — Cumulus: turbulence cloud field with a single warmer point
  cumulus: (rng, accent) => {
    const fid = 'art-cum-' + Math.floor(rng() * 1e6);
    const baseFreq = (0.008 + rng() * 0.012).toFixed(4);
    const sx = 30 + rng() * 40, sy = 30 + rng() * 40;
    return (
      <>
        <defs>
          <filter id={fid} x="0" y="0" width="100%" height="100%">
            <feTurbulence type="fractalNoise" baseFrequency={baseFreq} numOctaves="4" seed={Math.floor(rng() * 100)} />
            <feColorMatrix values="
              0 0 0 0 .85
              0 0 0 0 .85
              0 0 0 0 .82
              0 0 0 .9 -.35" />
          </filter>
          <radialGradient id={fid + '-spot'} cx={sx + '%'} cy={sy + '%'} r="22%">
            <stop offset="0%"  stopColor={accent || '#c7c4bd'} stopOpacity=".55" />
            <stop offset="100%" stopColor="transparent" />
          </radialGradient>
        </defs>
        <rect x="0" y="0" width="100" height="100" filter={`url(#${fid})`} opacity=".7" />
        <rect x="0" y="0" width="100" height="100" fill={`url(#${fid}-spot)`} />
      </>
    );
  },

  // C — Strata: horizontal bands at varied opacity
  strata: (rng, accent) => {
    const bands = 12 + Math.floor(rng() * 18);
    const horizon = 30 + rng() * 50;
    const fid = 'art-st-' + Math.floor(rng() * 1e6);
    return (
      <>
        <defs>
          <filter id={fid}>
            <feTurbulence type="fractalNoise" baseFrequency="0.9" numOctaves="2" seed={Math.floor(rng() * 100)} />
            <feColorMatrix values="0 0 0 0 .9  0 0 0 0 .9  0 0 0 0 .88  0 0 0 .25 -.1" />
            <feBlend in="SourceGraphic" mode="screen" />
          </filter>
          <linearGradient id={fid + '-g'} x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%" stopColor="#000" />
            <stop offset={(horizon - 8) + '%'} stopColor="#0a0d10" />
            <stop offset={horizon + '%'} stopColor={accent ? `color-mix(in oklab, ${accent} 25%, #1a1f25)` : '#1a1f25'} />
            <stop offset={(horizon + 4) + '%'} stopColor="#0e1115" />
            <stop offset="100%" stopColor="#000" />
          </linearGradient>
        </defs>
        <rect x="0" y="0" width="100" height="100" fill={`url(#${fid}-g)`} />
        {Array.from({ length: bands }).map((_, i) => {
          const y = (i / bands) * 100 + rng() * (100 / bands);
          const op = 0.04 + rng() * 0.14;
          return <rect key={i} x="0" y={y} width="100" height="0.4" fill="#cfcdc6" opacity={op} />;
        })}
        <rect x="0" y="0" width="100" height="100" filter={`url(#${fid})`} opacity=".25" />
      </>
    );
  },

  // D — Spire: vertical beam of light, top-down fade, grain overlay
  spire: (rng, accent) => {
    const cx = 30 + rng() * 40;
    const w = 10 + rng() * 25;
    const fid = 'art-sp-' + Math.floor(rng() * 1e6);
    return (
      <>
        <defs>
          <linearGradient id={fid + '-b'} x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%"  stopColor={accent || '#e7e7e6'} stopOpacity=".4" />
            <stop offset="60%" stopColor={accent || '#e7e7e6'} stopOpacity=".06" />
            <stop offset="100%" stopColor="#000" stopOpacity="0" />
          </linearGradient>
          <filter id={fid + '-blur'}>
            <feGaussianBlur stdDeviation="3" />
          </filter>
          <filter id={fid + '-grain'}>
            <feTurbulence type="fractalNoise" baseFrequency="1.1" numOctaves="2" seed={Math.floor(rng() * 100)} />
            <feColorMatrix values="0 0 0 0 .9  0 0 0 0 .9  0 0 0 0 .88  0 0 0 .35 -.15" />
          </filter>
        </defs>
        <rect x={cx - w / 2} y="0" width={w} height="100" fill={`url(#${fid}-b)`} filter={`url(#${fid}-blur)`} />
        <rect x="0" y="0" width="100" height="100" filter={`url(#${fid}-grain)`} opacity=".5" />
      </>
    );
  },

  // E — Particle field: scattered dots, denser in one corner
  particles: (rng, accent) => {
    const n = 240 + Math.floor(rng() * 200);
    const cx = rng() < .5 ? 20 + rng() * 20 : 60 + rng() * 20;
    const cy = rng() < .5 ? 20 + rng() * 20 : 60 + rng() * 20;
    const fid = 'art-p-' + Math.floor(rng() * 1e6);
    const pts = [];
    for (let i = 0; i < n; i++) {
      // bias toward (cx, cy)
      const bx = (rng() + rng() + rng()) / 3;
      const by = (rng() + rng() + rng()) / 3;
      const x = cx + (bx - 0.5) * 120;
      const y = cy + (by - 0.5) * 120;
      const r = 0.18 + rng() * 0.6;
      const op = 0.2 + rng() * 0.6;
      pts.push([x, y, r, op]);
    }
    return (
      <>
        <radialGradient id={fid + '-v'} cx={cx + '%'} cy={cy + '%'} r="80%">
          <stop offset="0%"  stopColor={accent ? `color-mix(in oklab, ${accent} 30%, #1a1d22)` : '#1a1d22'} />
          <stop offset="100%" stopColor="#000" />
        </radialGradient>
        <rect x="0" y="0" width="100" height="100" fill={`url(#${fid}-v)`} />
        {pts.map(([x, y, r, op], i) => (
          <circle key={i} cx={x} cy={y} r={r} fill="#e8e6df" opacity={op} />
        ))}
      </>
    );
  },

  // F — Eclipse: dark disc on subtle gradient, off-center
  eclipse: (rng, accent) => {
    const cx = 40 + rng() * 20;
    const cy = 40 + rng() * 20;
    const r  = 28 + rng() * 18;
    const fid = 'art-ec-' + Math.floor(rng() * 1e6);
    return (
      <>
        <defs>
          <radialGradient id={fid + '-bg'} cx="50%" cy="50%" r="70%">
            <stop offset="0%"  stopColor={accent ? `color-mix(in oklab, ${accent} 18%, #20242a)` : '#20242a'} />
            <stop offset="100%" stopColor="#06080a" />
          </radialGradient>
          <radialGradient id={fid + '-ring'} cx={cx + '%'} cy={cy + '%'} r={r + '%'}>
            <stop offset="0%"  stopColor="#04050a" />
            <stop offset="92%" stopColor="#04050a" />
            <stop offset="96%" stopColor={accent || '#a8a8a4'} stopOpacity=".8" />
            <stop offset="100%" stopColor="#04050a" stopOpacity="0" />
          </radialGradient>
          <filter id={fid + '-grain'}>
            <feTurbulence type="fractalNoise" baseFrequency="0.8" numOctaves="2" seed={Math.floor(rng() * 100)} />
            <feColorMatrix values="0 0 0 0 .9  0 0 0 0 .9  0 0 0 0 .88  0 0 0 .25 -.12" />
          </filter>
        </defs>
        <rect x="0" y="0" width="100" height="100" fill={`url(#${fid}-bg)`} />
        <circle cx={cx} cy={cy} r={r + 6} fill={`url(#${fid}-ring)`} />
        <rect x="0" y="0" width="100" height="100" filter={`url(#${fid}-grain)`} opacity=".35" style={{ mixBlendMode: 'overlay' }} />
      </>
    );
  },
};

const ARCHES = Object.keys(Archetypes); // 6 entries

// --- Public component ------------------------------------------------------

const Artwork = ({ seed = 'default', accent, style, className = '', corner, archetype }) => {
  const seedNum = hashSeed(String(seed));
  const rng = makeRng(seedNum);
  // First roll picks the archetype (consume one random first so changes
  // in later archetype code don't reshuffle which seed → which archetype)
  const pick = archetype || ARCHES[Math.floor(rng() * ARCHES.length)];
  const inner = Archetypes[pick](rng, accent);
  return (
    <div className={`artwork ${className}`} style={style}>
      <svg viewBox="0 0 100 100" preserveAspectRatio="xMidYMid slice" style={{ width: '100%', height: '100%', display: 'block' }}>
        <rect x="0" y="0" width="100" height="100" fill="#06080a" />
        {inner}
      </svg>
      {corner && <div className="artwork-corner">{corner}</div>}
    </div>
  );
};

window.MOTD_ART = { Artwork };
