import urequests as requests

def get_hourly(latitude, longitude):
    base_url = "https://api.open-meteo.com/v1/forecast"
    query = (
        f"latitude={latitude}&"
        f"longitude={longitude}&"
        "hourly=temperature_2m,precipitation_probability,weathercode&"
        "timezone=Europe%2FLondon"
    )
    full_url = f"{base_url}?{query}"

    try:
        response = requests.get(full_url)
        if response.status_code == 200:
            return response.json()
        else:
            print(f"Error: Status {response.status_code}")
            print(response.text)
            return None
    except Exception as e:
        print(f"Request failed: {e}")
        return None

def get_daily(latitude, longitude):
    base_url = "https://api.open-meteo.com/v1/forecast"
    query = (
        f"latitude={latitude}&"
        f"longitude={longitude}&"
        "daily=temperature_2m_max,temperature_2m_min,precipitation_sum,weathercode&"
        "timezone=Europe%2FLondon"
    )
    full_url = f"{base_url}?{query}"

    try:
        response = requests.get(full_url)
        if response.status_code == 200:
            return response.json()
        else:
            print(f"Error: Status {response.status_code}")
            print(response.text)
            return None
    except Exception as e:
        print(f"Request failed: {e}")
        return None

def get_today_forecast(data):
    daily = data["daily"]
    return {
        "date": daily["time"][0],
        "max_temp": daily["temperature_2m_max"][0],
        "min_temp": daily["temperature_2m_min"][0],
        "precip_mm": daily["precipitation_sum"][0],
        "weathercode": daily["weathercode"][0]
    }