const weatherService = require("../services/weatherService");
const openWeather = require("../network/openWeather");

exports.getWeather = async (_, response) => {
  let weatherData = await openWeather.getCurrentWeatherData(56600, "mx", "metric");

  response.header("Content-Type", "application/json");
  response.send(JSON.stringify(weatherData, null, 4));
}

exports.postWeather = async (request, response) => {
  const { zipCode, countryCode, tempMetric } = request.body;

  let weatherData = await openWeather.getCurrentWeatherData(zipCode, countryCode, tempMetric);

  response.header("Content-Type", "application/json");
  response.send(JSON.stringify(weatherData, null, 4));
};

exports.saveWeather = async (request, response) => {
  const { zipCode, countryCode, tempMetric } = request.body;
  let weatherData = await openWeather.getCurrentWeatherData(zipCode, countryCode, tempMetric);

  await weatherService.saveWeatherData(zipCode, weatherData);
  response.header("Content-Type", "application/json");
  response.send(JSON.stringify(weatherData, null, 4));
};

exports.getByQuery = async (request, response) => {
  const { zipCode } = request.query;

  let weatherData = await weatherService.getWeatherDataByZipCode(zipCode);
  response.header("Content-Type", "application/json");
  response.send(JSON.stringify(weatherData, null, 4));
};