/**
 * sun.ts — shared sun-rig math for the Three.js massing scenes.
 *
 * `sunFromHour` + `SunState` were lifted VERBATIM from MassingScene.tsx so the
 * ПТИЦА cockpit massing sandbox and the §7 «Концепция» 3D viewer share one
 * source of truth for the time-of-day → light-direction sweep (no copy-drift).
 *
 * Pure (no DOM / Three.js import) so it stays trivially unit-testable and can be
 * imported from any scene module.
 */

/** Sun slider hour bounds (06:00–20:00), shared by every massing scene. */
export const HOUR_MIN = 6;
export const HOUR_MAX = 20;

export interface SunState {
  x: number;
  y: number;
  z: number;
  elevation: number;
  warmth: number;
}

/** hour 6..20 → an east→south→west azimuth sweep plus an elevation arc. */
export function sunFromHour(hour: number): SunState {
  const h = Math.max(HOUR_MIN, Math.min(HOUR_MAX, hour));
  const t = (h - HOUR_MIN) / (HOUR_MAX - HOUR_MIN); // 0 at 06:00 → 1 at 20:00
  const elevation = Math.sin(t * Math.PI) * (Math.PI / 2) * 0.92 + 0.06;
  const azimuth = (t - 0.5) * Math.PI * 1.15;
  const R = 160;
  const cosEl = Math.cos(elevation);
  const x = Math.sin(azimuth) * cosEl * R;
  const z = Math.cos(azimuth) * cosEl * R * -1;
  const y = Math.sin(elevation) * R + 6;
  const warmth = 1 - Math.sin(t * Math.PI);
  return { x, y, z, elevation, warmth };
}
