/// file: public/assets/js/modules/home-diamond/dark-environment.js

/**
 * Bake a dark, contrasty PMREM for stylized reflections (cold luxury).
 * @param {typeof import('three')} THREE
 * @param {import('three').WebGLRenderer} renderer
 * @returns {{ texture: import('three').Texture; disposeBake: () => void }}
 */
export function createDarkEnvironment(THREE, renderer) {
  const pmrem = new THREE.PMREMGenerator(renderer);
  pmrem.compileEquirectangularShader();

  const envScene = new THREE.Scene();
  envScene.add(new THREE.AmbientLight(0x222c3c, 0.24));

  const key = new THREE.DirectionalLight(0xe8f0ff, 2.35);
  key.position.set(2.5, 5.5, 4);
  envScene.add(key);

  const cool = new THREE.DirectionalLight(0x557099, 0.72);
  cool.position.set(-6, 1, -3);
  envScene.add(cool);

  const rim = new THREE.DirectionalLight(0xb8d4ff, 0.58);
  rim.position.set(0, -2, -8);
  envScene.add(rim);

  const bakeDisposables = [];
  const addProbe = (color, emissive, x, y, z, scale) => {
    const g = new THREE.SphereGeometry(scale, 20, 20);
    const m = new THREE.MeshStandardMaterial({
      color,
      emissive,
      emissiveIntensity: 1.45,
      roughness: 0.55,
      metalness: 0.2,
    });
    const mesh = new THREE.Mesh(g, m);
    mesh.position.set(x, y, z);
    envScene.add(mesh);
    bakeDisposables.push(g, m);
  };

  addProbe(0x2a3548, 0x0a1428, 7, 3, 5, 1.1);
  addProbe(0x334455, 0x112233, -6, -2, 4, 0.85);
  addProbe(0x3a4a5c, 0x1a3048, 2, -4, -5, 0.7);

  const rt = pmrem.fromScene(envScene, 0.022);
  pmrem.dispose();

  const disposeBake = () => {
    for (const d of bakeDisposables) {
      d.dispose();
    }
    rt.dispose();
  };

  return { texture: rt.texture, disposeBake };
}
