/**
 * Open-Meteo REST API wrapper for fetching weather details based on coordinates.
 */
export const fetchCurrentWeather = async (
  latitude: number,
  longitude: number
): Promise<{ temp: string; icon: string }> => {
  try {
    const response = await fetch(
      `https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}&current_weather=true`
    );
    const data = await response.json();
    const temp = data?.current_weather?.temperature;
    
    // Map weather codes to Ionicons if needed, using simplified icons for now
    const weatherCode = data?.current_weather?.weathercode;
    let icon = 'cloud-outline';
    if (weatherCode === 0) icon = 'sunny-outline';
    else if (weatherCode >= 1 && weatherCode <= 3) icon = 'partly-sunny-outline';
    else if (weatherCode >= 51 && weatherCode <= 67) icon = 'rainy-outline';
    else if (weatherCode >= 71 && weatherCode <= 77) icon = 'snow-outline';
    else if (weatherCode >= 80 && weatherCode <= 82) icon = 'thunderstorm-outline';

    return {
      temp: temp !== undefined ? `${Math.round(temp)}°C` : '26°C',
      icon,
    };
  } catch (error) {
    console.error('Lỗi khi tải thời tiết:', error);
    return { temp: '26°C', icon: 'cloud-outline' };
  }
};
