//! BigDataCloud — Network Engineering Sample
//! Run: BIGDATACLOUD_API_KEY=your-key cargo run --example network_engineering

use bigdatacloud::Client;

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

    println!("=== ASN Info Extended (AS13335) ===");
    let asn = client.network_engineering.get_asn_info_full("AS13335", "en").unwrap();
    println!("ASN:              {}", asn.asn.as_deref().unwrap_or(""));
    println!("Organisation:     {}", asn.organisation.as_deref().unwrap_or(""));
    println!("Total Receiving:  {}", asn.total_receiving_from.unwrap_or(0));
    println!("Total Transit To: {}", asn.total_transit_to.unwrap_or(0));
    println!("IPv4 Addresses:   {}", asn.total_ipv4_addresses.unwrap_or(0));
    println!("Rank:             {}", asn.rank_text.as_deref().unwrap_or(""));
    println!("Upstream providers (top 3):");
    for peer in asn.receiving_from.as_deref().unwrap_or(&[]).iter().take(3) {
        println!("  {} — {} ({})", peer.asn.as_deref().unwrap_or(""), peer.organisation.as_deref().unwrap_or(""), peer.registered_country.as_deref().unwrap_or(""));
    }

    println!("\n=== ASN Receiving From (page 1) ===");
    let rf = client.network_engineering.get_receiving_from("AS13335", 5, 0, "en").unwrap();
    println!("Total: {}", rf.total_receiving_from.unwrap_or(0));
    for peer in rf.receiving_from.as_deref().unwrap_or(&[]) {
        println!("  {} — {}", peer.asn.as_deref().unwrap_or(""), peer.organisation.as_deref().unwrap_or(""));
    }

    println!("\n=== ASN Transit To (page 1) ===");
    let tt = client.network_engineering.get_transit_to("AS13335", 5, 0, "en").unwrap();
    println!("Total: {}", tt.total_transit_to.unwrap_or(0));
    for peer in tt.transit_to.as_deref().unwrap_or(&[]) {
        println!("  {} — {}", peer.asn.as_deref().unwrap_or(""), peer.organisation.as_deref().unwrap_or(""));
    }

    println!("\n=== BGP Prefixes (AS13335, first 5) ===");
    let prefixes = client.network_engineering.get_bgp_prefixes("AS13335", true, 5, 0).unwrap();
    println!("Total: {}", prefixes.total.unwrap_or(0));
    for p in prefixes.prefixes.as_deref().unwrap_or(&[]) {
        println!("  {}  {} — {}", p.bgp_prefix.as_deref().unwrap_or(""), p.bgp_prefix_network_address.as_deref().unwrap_or(""), p.bgp_prefix_last_address.as_deref().unwrap_or(""));
    }

    println!("\n=== Networks by CIDR ===");
    let net = client.network_engineering.get_networks_by_cidr("1.1.1.0/24", "en").unwrap();
    println!("CIDR:   {}", net.cidr.as_deref().unwrap_or(""));
    println!("Parent: {}", net.parent.as_deref().unwrap_or(""));
    if let Some(n) = &net.network {
        println!("Organisation: {}", n.organisation.as_deref().unwrap_or(""));
        println!("Registry:     {}", n.registry.as_deref().unwrap_or(""));
        println!("Country:      {}", n.registered_country_name.as_deref().unwrap_or(""));
    }

    println!("\n=== Top 5 ASNs by IPv4 Space ===");
    let ranks = client.network_engineering.get_asn_rank_list(5, 0).unwrap();
    println!("Total ASNs: {}", ranks.total.unwrap_or(0));
    for a in ranks.asns.as_deref().unwrap_or(&[]) {
        println!("  #{} {} — {} ({}) — {} IPs", a.rank.unwrap_or(0), a.asn.as_deref().unwrap_or(""), a.organisation.as_deref().unwrap_or(""), a.registered_country.as_deref().unwrap_or(""), a.total_ipv4_addresses.unwrap_or(0));
    }

    println!("\n=== Tor Exit Nodes (first 5) ===");
    let tor = client.network_engineering.get_tor_exit_nodes(5, 0).unwrap();
    println!("Total active nodes: {}", tor.total.unwrap_or(0));
    for node in tor.nodes.as_deref().unwrap_or(&[]) {
        println!("  {} — {} ({})", node.ip.as_deref().unwrap_or(""), node.country_name.as_deref().unwrap_or(""), node.country_code.as_deref().unwrap_or(""));
    }
}
