/**
 * @copyright nhcarrigan
 * @license Naomi's Public License
 * @author Naomi Carrigan
 */
import { temperatures } from "../data/weather/temperatures.js";
import { winds } from "../data/weather/winds.js";
import { errorHandler } from "../utils/errorHandler.js";
import { getRandomValue } from "./getRandomValue.js";
import { getPrecipitation } from "./weather/getPrecipitation.js";
import { getRegionRestrictions } from "./weather/getRegionRestrictions.js";
import { getSeason } from "./weather/getSeason.js";
import { getSpecial } from "./weather/getSpecial.js";
import type { RegionName } from "../interfaces/weather/names/regionName.js";
import type { WeatherForecast } from "../interfaces/weather/weatherForecast.js";

/**
 * Generates a weather forecast for the specified region, using the current
 * date to determine the season.
 * @param region - The name of the region to forecast for. Must be one of "Rudania", "Inariko", or "Vhintl".
 * @returns The weather forecast.
 */
export const getWeatherForecast = (
  region: RegionName,
): WeatherForecast | null => {
  try {
    const season = getSeason();
    const allowedWeather = getRegionRestrictions(region, season);

    if (!allowedWeather) {
      return null;
    }

    const temporaryName = getRandomValue(allowedWeather.temps);
    const temperature = temperatures.find((element) => {
      return element.name === temporaryName;
    });
    const windName = getRandomValue(allowedWeather.wind, "low");
    const wind = winds.find((element) => {
      return element.name === windName;
    });

    if (!temperature || !wind) {
      return null;
    }

    const precipitation = getPrecipitation(allowedWeather, temporaryName, windName);
    const special = getSpecial(
      allowedWeather,
      temporaryName,
      windName,
      precipitation?.name,
    );

    return { precipitation, region, season, special, temperature, wind };
  } catch (error) {
    void errorHandler(error, "get weather forecast");
    return null;
  }
};
