/* Weather Squatch — shared config, clip map, scene layer spec, mock broadcast.
   Everything attaches to window so the Babel-transpiled modules can share scope. */

// ─── Model + animation clips ────────────────────────────────────────────────
// Served from the root app at /weather-squatch/assets/weather-squatch.glb. The
// loader tries MODEL_URL first, then MODEL_URL_FALLBACK.
const MODEL_URL = "/weather-squatch/assets/weather-squatch.glb";
const MODEL_URL_FALLBACK = "weather-squatch/assets/weather-squatch.glb";

// The clip NAMES in this GLB are mislabeled — diagnosed each clip by its actual
// Hips motion + visual pose and remapped to semantic roles by INDEX:
//   0 = sits on ground        1 = short standing idle    2 = lying down
//   3 = gets up from ground   4 = wave hello             5 = calm standing idle (long, loops)
//   6 = looks around          7 = walk stride
const ROLE_INDEX = {
  idle:   5,   // calm standing idle — the loop
  wave:   4,   // big wave hello
  look:   6,   // looks around (curious/dumbfounded)
  getUp:  3,   // rises from the ground to standing
  walk:   7,   // walk stride (played in place)
  sit:    0,   // sits on the ground
  lie:    2,   // lying down
  standA: 1,   // alternate short standing idle
};
function getClip(gltf, role) {
  const a = gltf.animations || [];
  return a[ROLE_INDEX[role]] || a[0];
}

// ─── 2D parallax layers (back → front) ───────────────────────────────────────
// speed = parallax scroll multiplier. Faster layers are rendered wider so they
// never run out during the walk. Each loads /weather-squatch/assets/{name}.png
// and falls back to a labeled placeholder until the real art is dropped in.
const ASSET_BASE = "/weather-squatch/assets/";
const LAYERS = [
  { name: "sky",             speed: 0.0,  z: 1,  color: "#bfe0ef", label: "SKY",             role: "gradient" },
  { name: "clouds",          speed: 0.1,  z: 2,  color: "#eef6fb", label: "CLOUDS",          role: "clouds"   },
  { name: "treeline-far",    speed: 0.25, z: 3,  color: "#9fc59a", label: "TREELINE · FAR",  role: "ridge", h: 46 },
  { name: "trees-mid",       speed: 0.5,  z: 4,  color: "#7bae79", label: "TREES · MID",     role: "ridge", h: 60 },
  { name: "meadow",          speed: 0.7,  z: 5,  color: "#a9cc73", label: "MEADOW",          role: "ground", h: 42 },
  { name: "pond",            speed: 0.7,  z: 6,  color: "#8fc2d6", label: "POND",            role: "pond" },
  { name: "grass-foreground",speed: 1.0,  z: 8,  color: "#6f9e44", label: "GRASS · FOREGROUND", role: "grass", h: 24 },
  { name: "pass-tree",       speed: 1.3,  z: 9,  color: "#4d6b3a", label: "PASS-TREE",       role: "passtree" },
];

// How far (px) the meadow (speed 0.7) layer travels during THE WALK.
const SCROLL_DISTANCE = 1500;

// ─── Broadcast API ───────────────────────────────────────────────────────────
const BROADCAST_ENDPOINT = "/api/weather-squatch/broadcast";

// Small ZIP → place table for the offline mock. Unknown (valid) ZIPs get a
// procedurally-named hollow so the demo always resolves to something charming.
const MOCK_PLACES = {
  "97201": ["Portland", "OR"],
  "98101": ["Seattle", "WA"],
  "94103": ["San Francisco", "CA"],
  "10001": ["New York", "NY"],
  "80202": ["Denver", "CO"],
  "00000": ["Bigfoot Hollow", "OR"],
};
const MOCK_404 = new Set(["99999"]); // pretend this one doesn't exist

function mockBroadcast(zip) {
  if (MOCK_404.has(zip)) {
    const e = new Error("not found"); e.status = 404; throw e;
  }
  const [city, state] = MOCK_PLACES[zip] || ["Sasquatch Springs", "OR"];
  const periods = [
    { name: "This Afternoon", temperature: 68, temperatureUnit: "F", shortForecast: "Sunny with gentle clouds", windSpeed: "5 mph", windDirection: "W" },
    { name: "Tonight",        temperature: 51, temperatureUnit: "F", shortForecast: "Clear and cool",            windSpeed: "3 mph", windDirection: "NW" },
    { name: "Tomorrow",       temperature: 72, temperatureUnit: "F", shortForecast: "Mostly sunny",              windSpeed: "6 mph", windDirection: "SW" },
  ];
  const script =
    `Well now, lemme tell ya about ${city}, ${state}. ` +
    `This afternoon she's a real peach — sunny, about sixty-eight degrees, ` +
    `with a gentle little breeze comin' outta the west. ` +
    `Come nightfall it cools right down to the low fifties, clear as a mountain spring. ` +
    `And tomorrow? Tomorrow's a beauty, little buddy — seventy-two and mostly sunny. ` +
    `So go on out there and enjoy it. Squatch says so.`;
  return { script, audioBase64: null, city, state, periods };
}

Object.assign(window, {
  MODEL_URL, MODEL_URL_FALLBACK, ROLE_INDEX, getClip,
  ASSET_BASE, LAYERS, SCROLL_DISTANCE,
  BROADCAST_ENDPOINT, mockBroadcast,
});
