import { loadPMREM, onStart, type Context } from "@needle-tools/engine";

const NEEDLE_HDRI_BASE = "https://cdn.needle.tools/static/hdris";

/**
 * Single outdoor sky HDRI for the whole menu (FastHDR .ktx2 on Needle CDN).
 * "Overcast Soil Puresky" — soft overcast sky, neutral lighting for all dishes.
 * @see https://cloud.needle.tools/hdris
 */
export const MENU_SKYBOX_URL = `${NEEDLE_HDRI_BASE}/overcast_soil_puresky_2k.pmrem.ktx2`;

let applied = false;

/** Apply shared sky background + image-based lighting once for the menu scene. */
export async function applyMenuSkybox(context: Context): Promise<void> {
  if (applied) return;

  const tex = await loadPMREM(MENU_SKYBOX_URL, context.renderer);
  if (!tex) return;

  applied = true;
  context.scene.background = tex;
  context.scene.environment = tex;
}

onStart((context) => {
  void applyMenuSkybox(context);
});
