import fetch from "node-fetch";
import WeatherService, { Forecast } from "../entities/weatherService";

export default class OpenWeatherService implements WeatherService {
  constructor(
    private readonly appid: string,
    private readonly units: string = "metric"
  ) {}

  async getForecastFor(lat: number, lon: number): Promise<Forecast> {
    const { weather, main } = await fetch(
      `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${this.appid}&units=${this.units}`
    ).then((r) => r.json());

    return {
      temperature: main.temp,
      min: main.temp_min,
      max: main.temp_max,
      weather: weather[0].main,
    };
  }
}
