import geopandas as gpd
import pandas as pd
import matplotlib.pyplot as plt
from shapely.geometry import Point

capitals = {
    'Paris': Point(2.3522, 48.8566),
    'Berlin': Point(13.4050, 52.5200),
    'Madrid': Point(-3.7038, 40.4168),
    'Rome': Point(12.4964, 41.9028),
    'London': Point(-0.1278, 51.5074)
}
capitals_gdf = gpd.GeoDataFrame(
    [{'city': city, 'geometry': point} for city, point in capitals.items()],
    crs='EPSG:4326'
)
capitals_proj = capitals_gdf.to_crs('EPSG:3857')  # Web Mercator
print([{'city': city, 'geometry': point} for city, point in capitals.items()])
# Alle Distanzen zwischen Städten berechnen
print(capitals_proj)

print("Distances between cities")
for i, city1 in capitals_proj.iterrows():
    for j, city2 in capitals_proj.iterrows():
        if i >= j:
            continue
        dist_m = city1["geometry"].distance(city2["geometry"])
        dist_km = dist_m / 1_000
        print(f"Distance between {city1['city']} and {city2['city']} = {dist_km:.0f}km")

print()

# buffer
world = gpd.read_file(
    "../../data/110m_cultural.zip",
    layer="ne_110m_admin_0_countries")

switzerland = world[world['NAME'] == 'Switzerland']
if len(switzerland) > 0:
    # Project to equal-area for accurate buffering
    switzerland_proj = switzerland.to_crs('EPSG:3857')
    buffer_200km = switzerland_proj.geometry.buffer(900_000)  # 200km in meters

    buffer_gdf = gpd.GeoDataFrame(geometry=buffer_200km.to_crs('EPSG:4326'))

    # Find countries that intersect with this buffer
    countries_near_switzerland = gpd.sjoin(world, buffer_gdf, how='inner', predicate='intersects')
    nearby_countries = countries_near_switzerland['NAME'].tolist()
    print(f"Countries within 200km of Switzerland: {nearby_countries}")
