//! Example showing various cache purging operations.

use lmrc_cloudflare::CloudflareClient;

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

    // Create client
    println!("Connecting to Cloudflare...");
    let client = CloudflareClient::new(api_token)?;

    // Get zone ID
    let zone_id = client.zones().get_zone_id(&zone_name).await?;
    println!("Found zone: {} ({})", zone_name, zone_id);

    // Example 1: Purge specific URLs
    println!("\n=== Purging specific URLs ===");
    let urls = vec![
        format!("https://{}/page1", zone_name),
        format!("https://{}/page2", zone_name),
    ];

    let result = client
        .cache()
        .purge_urls(&zone_id)
        .urls(urls.clone())
        .send()
        .await?;

    println!("Purged URLs: {:?}", urls);
    println!("Purge ID: {}", result.id);

    // Example 2: Purge everything (commented out for safety)
    /*
    println!("\n=== Purging everything ===");
    let result = client.cache()
        .purge_everything(&zone_id)
        .await?;

    println!("Purged all cache!");
    println!("Purge ID: {}", result.id);
    */

    // Example 3: Purge by cache tags (Enterprise plan only)
    // Uncomment if you have an Enterprise plan
    /*
    println!("\n=== Purging by cache tags ===");
    let result = client.cache()
        .purge_tags(&zone_id)
        .tags(vec!["product", "blog"])
        .send()
        .await?;

    println!("Purged cache by tags!");
    println!("Purge ID: {}", result.id);
    */

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