//! Basic example showing common operations with the Cloudflare client.

use lmrc_cloudflare::CloudflareClient;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Get API token from environment
    let api_token = std::env::var("CLOUDFLARE_API_TOKEN")
        .expect("CLOUDFLARE_API_TOKEN environment variable must be set");

    // Create client
    println!("Creating Cloudflare client...");
    let client = CloudflareClient::builder().api_token(api_token).build()?;

    // List all zones
    println!("\n=== Listing zones ===");
    let zones = client.zones().list().send().await?;
    for zone in &zones {
        println!("  - {} ({})", zone.name, zone.id);
    }

    if zones.is_empty() {
        println!("No zones found!");
        return Ok(());
    }

    // Use the first zone for demonstration
    let zone = &zones[0];
    let zone_id = &zone.id;

    // List DNS records
    println!("\n=== Listing DNS records for {} ===", zone.name);
    let records = client.dns().list_records(zone_id).send().await?;

    for record in &records {
        println!(
            "  - {} ({}) -> {} [proxied: {}]",
            record.name, record.record_type, record.content, record.proxied
        );
    }

    // Example: Create an A record (commented out to avoid making actual changes)
    /*
    println!("\n=== Creating a DNS record ===");
    let new_record = client.dns()
        .create_record(zone_id)
        .name("test.example.com")
        .record_type(RecordType::A)
        .content("192.0.2.1")
        .proxied(true)
        .ttl(1)
        .comment("Created by cloudflare-client example")
        .send()
        .await?;

    println!("Created record: {} ({})", new_record.name, new_record.id);

    // Update the record
    println!("\n=== Updating the DNS record ===");
    let updated_record = client.dns()
        .update_record(zone_id, &new_record.id)
        .content("192.0.2.2")
        .send()
        .await?;

    println!("Updated record content to: {}", updated_record.content);

    // Delete the record
    println!("\n=== Deleting the DNS record ===");
    client.dns()
        .delete_record(zone_id, &new_record.id)
        .await?;

    println!("Record deleted successfully");
    */

    println!("\n=== Done! ===");
    Ok(())
}
