//! BigDataCloud — Reverse Geocoding Sample
//! Run: BIGDATACLOUD_API_KEY=your-key cargo run --example reverse_geocoding

use bigdatacloud::Client;

fn main() {
    let client = Client::from_environment().expect("Failed to create client");

    println!("=== Reverse Geocode ===");
    let r = client.reverse_geocoding.reverse_geocode(-33.8688, 151.2093, "en").unwrap();
    println!("City:        {}", r.city.as_deref().unwrap_or(""));
    println!("Locality:    {}", r.locality.as_deref().unwrap_or(""));
    println!("Subdivision: {} ({})", r.principal_subdivision.as_deref().unwrap_or(""), r.principal_subdivision_code.as_deref().unwrap_or(""));
    println!("Country:     {} ({})", r.country_name.as_deref().unwrap_or(""), r.country_code.as_deref().unwrap_or(""));
    println!("Postcode:    {}", r.postcode.as_deref().unwrap_or(""));
    println!("Plus Code:   {}", r.plus_code.as_deref().unwrap_or(""));
    if let Some(info) = &r.locality_info {
        println!("Administrative hierarchy:");
        for area in info.administrative.as_deref().unwrap_or(&[]) {
            println!("  Level {}: {} ({})", area.admin_level.unwrap_or(0), area.name.as_deref().unwrap_or(""), area.iso_code.as_deref().unwrap_or(""));
        }
    }

    println!("\n=== Reverse Geocode with Timezone ===");
    let rt = client.reverse_geocoding.reverse_geocode_with_timezone(35.6762, 139.6503, "en").unwrap();
    println!("City:       {}", rt.city.as_deref().unwrap_or(""));
    println!("Country:    {}", rt.country_name.as_deref().unwrap_or(""));
    if let Some(tz) = &rt.time_zone {
        println!("Timezone:   {}", tz.iana_time_id.as_deref().unwrap_or(""));
        println!("UTC Offset: {}", tz.utc_offset.as_deref().unwrap_or(""));
        println!("DST:        {}", tz.is_daylight_saving_time.unwrap_or(false));
        println!("Local Time: {}", tz.local_time.as_deref().unwrap_or(""));
    }

    println!("\n=== Reverse Geocode in Japanese ===");
    let ja = client.reverse_geocoding.reverse_geocode(35.6762, 139.6503, "ja").unwrap();
    println!("City:        {}", ja.city.as_deref().unwrap_or(""));
    println!("Subdivision: {}", ja.principal_subdivision.as_deref().unwrap_or(""));
    println!("Country:     {}", ja.country_name.as_deref().unwrap_or(""));

    println!("\n=== Timezone by Location ===");
    let tz = client.reverse_geocoding.get_timezone_by_location(-33.8688, 151.2093).unwrap();
    println!("IANA ID:    {}", tz.iana_time_id.as_deref().unwrap_or(""));
    println!("Display:    {}", tz.display_name.as_deref().unwrap_or(""));
    println!("UTC Offset: {}", tz.utc_offset.as_deref().unwrap_or(""));
    println!("Local Time: {}", tz.local_time.as_deref().unwrap_or(""));
}
