// Gurugram coordinates (product is explicitly Gurugram-targeted)
const LAT = 28.4595;
const LON = 77.0266;

export interface WeatherSnapshot {
  tempC: number;
  description: string;
  season: "summer" | "monsoon" | "winter";
}

// Maps temperature band to Gurugram season label.
// < 15°C  → winter
// 15–28°C → monsoon (shoulder / rain season)
// > 28°C  → summer
function tempToSeason(tempC: number): "summer" | "monsoon" | "winter" {
  if (tempC < 15) return "winter";
  if (tempC <= 28) return "monsoon";
  return "summer";
}

export async function getGurgaonWeather(): Promise<WeatherSnapshot> {
  try {
    const url = `https://api.open-meteo.com/v1/forecast?latitude=${LAT}&longitude=${LON}&current_weather=true`;
    const res = await fetch(url, { next: { revalidate: 1800 } }); // cache 30 min
    if (!res.ok) throw new Error(`weather API ${res.status}`);
    const data = await res.json();
    const tempC: number = data.current_weather?.temperature ?? 30;
    const season = tempToSeason(tempC);
    const descriptions: Record<string, string> = {
      summer: "hot",
      monsoon: "warm",
      winter: "cool",
    };
    return {
      tempC,
      description: descriptions[season],
      season,
    };
  } catch {
    // Fallback to summer (Gurugram default)
    return { tempC: 35, description: "hot", season: "summer" };
  }
}
