use std::collections::HashMap;
use crate::http_client::HttpClient;
use crate::error::Result;
use crate::models::{ReverseGeocodeResponse, ReverseGeocodeWithTimezoneResponse, TimezoneResponse};

/// Reverse Geocoding package — 3 endpoints.
#[derive(Clone)]
pub struct ReverseGeocodingApi {
    http: HttpClient,
}

impl ReverseGeocodingApi {
    pub fn new(http: HttpClient) -> Self { Self { http } }

    /// Converts GPS coordinates to city, locality, subdivision, and country.
    pub fn reverse_geocode(&self, latitude: f64, longitude: f64, locality_language: &str) -> Result<ReverseGeocodeResponse> {
        self.http.get("reverse-geocode", latlon(latitude, longitude, locality_language))
    }

    /// Converts GPS coordinates to locality and timezone in a single call.
    pub fn reverse_geocode_with_timezone(&self, latitude: f64, longitude: f64, locality_language: &str) -> Result<ReverseGeocodeWithTimezoneResponse> {
        self.http.get("reverse-geocode-with-timezone", latlon(latitude, longitude, locality_language))
    }

    /// Returns timezone information for GPS coordinates.
    pub fn get_timezone_by_location(&self, latitude: f64, longitude: f64) -> Result<TimezoneResponse> {
        let mut p = HashMap::new();
        p.insert("latitude".to_string(), latitude.to_string());
        p.insert("longitude".to_string(), longitude.to_string());
        self.http.get("timezone-by-location", p)
    }
}

fn latlon(lat: f64, lon: f64, lang: &str) -> HashMap<String, String> {
    let mut p = HashMap::new();
    p.insert("latitude".to_string(), lat.to_string());
    p.insert("longitude".to_string(), lon.to_string());
    p.insert("localityLanguage".to_string(), lang.to_string());
    p
}
