/**
 * Season cycle: 4 seasons from game day. Used for temperature, visitors, reproduction, billeterie.
 * Refs: docs/specs/inventaire_saisons.md, temperature.md, visiteur.md, reproduction.md, billeterie.md.
 */

import { GameConfig } from "./config.js";

/** @typedef {"spring"|"summer"|"autumn"|"winter"} SeasonId */

const SEASON_ORDER = /** @type {SeasonId[]} */ (["spring", "summer", "autumn", "winter"]);

/**
 * Current season from state game day.
 * @param {import("./types.js").GameState} state
 * @returns {SeasonId}
 */
export function getCurrentSeason(state) {
  const cfg = GameConfig.Season;
  if (!cfg || !cfg.DaysPerSeason) return "spring";
  const gameDay = state.gameDayTotal ?? 0;
  const seasonIndex = Math.floor(gameDay / cfg.DaysPerSeason) % 4;
  return SEASON_ORDER[seasonIndex] ?? "spring";
}

/**
 * Day index within current season (0..DaysPerSeason-1).
 * @param {import("./types.js").GameState} state
 * @returns {number}
 */
export function getSeasonDay(state) {
  const cfg = GameConfig.Season;
  if (!cfg || !cfg.DaysPerSeason) return 0;
  const gameDay = state.gameDayTotal ?? 0;
  return gameDay % cfg.DaysPerSeason;
}

/**
 * Temperature modifier for display/calculations (°C). From temperature.md.
 * @param {SeasonId} season
 * @returns {number}
 */
export function getSeasonTemperatureModifier(season) {
  const cfg = GameConfig.Season?.TemperatureModifier;
  if (!cfg) return 0;
  return cfg[season] ?? 0;
}

/**
 * Visitor demand multiplier. From visiteur.md (Impact Saisons).
 * @param {SeasonId} season
 * @returns {number}
 */
export function getSeasonVisitorMultiplier(season) {
  const cfg = GameConfig.Season?.VisitorMultiplier;
  if (!cfg) return 1;
  return cfg[season] ?? 1;
}

/**
 * Reproduction chance bonus (e.g. spring +0.2, winter -0.5). From reproduction.md.
 * @param {SeasonId} season
 * @returns {number}
 */
export function getSeasonReproductionBonus(season) {
  const cfg = GameConfig.Season?.ReproductionBonus;
  if (!cfg) return 0;
  return cfg[season] ?? 0;
}

/**
 * Billeterie ticket price multiplier (summer +20%, winter -10%). From billeterie.md.
 * @param {SeasonId} season
 * @returns {number}
 */
export function getSeasonTicketPriceMultiplier(season) {
  const cfg = GameConfig.Season?.TicketPriceMultiplier;
  if (!cfg) return 1;
  return cfg[season] ?? 1;
}
