
import requests
from pprint import pprint

API_KEY = "5559491e991a00f95a22b5ee57ff1fa0"

def check_coordinates(city, API_KEY):
    response = requests.get(
        f"https://api.openweathermap.org/geo/1.0/direct?q={city}&limit=1&appid={API_KEY}"
    )
    if response.status_code != 200 or not response.json():
        print(f"Błąd: nie można znaleźć miasta {city}")
        return None

    data = response.json()[0]
    lat = data['lat']
    lon = data['lon']
    city = data['name']
    country = data['country']
    return lat, lon, city, country

def get_weather_info(lat, lon):
    response = requests.get(
        f"https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={API_KEY}&lang=pl&units=metric"
    )
    if response.status_code != 200:
        print("Nie udało się pobrać pogody.")
        return
    data = response.json()
    pprint(data)

# Główna logika programu
print("Witaj, jestem Travelinator – twój inteligentny asystent podróży!")

origin_city_input = input("Podaj nazwę miasta, z którego podróżujesz: ")
destination_input = input("Podaj nazwę miasta, do którego podróżujesz: ")

# Współrzędne miast
origin = check_coordinates(origin_city_input, API_KEY)
destination = check_coordinates(destination_input, API_KEY)

if origin and destination:
    origin_lat, origin_lon, origin_city, origin_country = origin
    destination_lat, destination_lon, destination_city, destination_country = destination

    print(f"\n📍 Miasto początkowe: {origin_city} ({origin_country})")
    print(f"📍 Miasto docelowe: {destination_city} ({destination_country})")
    print(f"🌍 Współrzędne celu: szerokość {destination_lat}, długość {destination_lon}")

    print("\n🌤 Aktualna pogoda w miejscu docelowym:")
    get_weather_info(destination_lat, destination_lon)