import Temperature from "./Temperature";
import { Forecast, LocationType } from "@/types/types";

async function getForecast(location: LocationType): Promise<Forecast> {

  const { latitude, longtitude } = location;
  const appId = "546911d860cb81f16585f7973b394b70";
  const res = await fetch(
    `https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longtitude}&appid=${appId}`
  );
  if (!res.ok) {
    throw new Error(`Failed to fetch the weather data`);
  }
  return res.json();
}

export default async function WeatherNow(props: { location: LocationType }) {
  const location = props.location;
  const weather = await getForecast(location);
  return (
    <main>
      <h1>Forecast</h1>
      <Temperature tempInfo={weather.main}/>
      <p></p>
    </main>
  );
}
