import { useQuery } from '@tanstack/react-query'

// {"id":1001,"city":"Salem","temp":66.7,"feels_like":67.17,"humidity":87,"conditions":"scattered clouds","fetched_at":"2026-02-18T22:16:38.067097Z"}
interface WeatherHistory {
    id: number;
    city: string;
    temp: number;
    feels_like: number;
    humidity: number;
    conditions: string;
    fetched_at: string;
}

async function fetchWeatherHistory(): Promise<WeatherHistory[]>{
    const res = await fetch("/api/weather/history");
    if(!res.ok) throw new Error("Failed to fetch weather history");
    return res.json();
}

function WeatherHistory() {
    const {data, isLoading, error} = useQuery({
        queryKey: ["weather-history"],
        queryFn: fetchWeatherHistory,

    });
    return {data, isLoading, error}
}

export default WeatherHistory
