import { env } from '$env/dynamic/public';

// --- "passport stamps" reverse geocoding -----------------------------------
// Reverse-geocodes a lat/lng pair into a { city, country } pair using the
// Google Maps Geocoding API (REST), reusing the same PUBLIC_MAPS_KEY the
// Maps JavaScript API loader already uses (see MapView.svelte) - the
// Geocoding API must be enabled for this key in the Google Cloud Console for
// this to work.
//
// Walks the first result's address_components looking for the 'locality'
// type for the city name (falling back to 'administrative_area_level_1',
// e.g. a state/province, for rural areas with no locality) and the 'country'
// type for the country name. Returns { city: 'Unknown', country: 'Unknown' }
// if the request fails or no results come back, so a failed lookup never
// blocks stamp creation - it just produces a generically-labeled stamp.
export async function reverseGeocode(lat, lng) {
    try {
        const url = `https://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lng}&key=${env.PUBLIC_MAPS_KEY}`;
        const res = await fetch(url);
        const data = await res.json();

        const components = data.results?.[0]?.address_components ?? [];
        const findType = (type) => components.find(c => c.types.includes(type))?.long_name;

        return {
            city: findType('locality') ?? findType('administrative_area_level_1') ?? 'Unknown',
            country: findType('country') ?? 'Unknown'
        };
    } catch (err) {
        console.error('[stamps] reverse geocode failed:', err);
        return { city: 'Unknown', country: 'Unknown' };
    }
}
