//! Test URL generation fix
//!
//! This example demonstrates that the double slash bug has been fixed

use std::collections::HashMap;
use serde_json::json;
use comfyui_sdk::{HTTPClient, ComfyUIClientConfig};

fn main() {
    println!("🔧 Testing URL Generation Fix");
    println!("==============================");

    // Test with base URL ending with slash (the problematic case)
    let config_with_slash = ComfyUIClientConfig {
        base_url: "http://192.168.0.193:8188/".to_string(),
        ..Default::default()
    };
    let client_with_slash = HTTPClient::new(config_with_slash).unwrap();

    // Test with base URL without ending slash
    let config_without_slash = ComfyUIClientConfig {
        base_url: "http://192.168.0.193:8188".to_string(),
        ..Default::default()
    };
    let client_without_slash = HTTPClient::new(config_without_slash).unwrap();

    // Create test outputs (simulating ComfyUI response)
    let mut outputs = HashMap::new();
    outputs.insert("58".to_string(), json!({
        "images": [
            {
                "filename": "ComfyUI_02046_.png",
                "subfolder": "",
                "type": "output"
            }
        ]
    }));

    println!("\n📋 Test Case 1: Base URL with trailing slash");
    println!("Base URL: http://192.168.0.193:8188/");
    let urls_with_slash = client_with_slash.outputs_to_urls(&outputs);
    for url in &urls_with_slash {
        println!("Generated URL: {}", url);
        if url.contains("//view") {
            println!("❌ FAILED: URL contains double slash!");
        } else {
            println!("✅ PASSED: No double slash found");
        }
    }

    println!("\n📋 Test Case 2: Base URL without trailing slash");
    println!("Base URL: http://192.168.0.193:8188");
    let urls_without_slash = client_without_slash.outputs_to_urls(&outputs);
    for url in &urls_without_slash {
        println!("Generated URL: {}", url);
        if url.contains("//view") {
            println!("❌ FAILED: URL contains double slash!");
        } else {
            println!("✅ PASSED: No double slash found");
        }
    }

    println!("\n📋 Test Case 3: Multiple images");
    let mut multi_outputs = HashMap::new();
    multi_outputs.insert("58".to_string(), json!({
        "images": [
            {
                "filename": "image1.png",
                "subfolder": "temp",
                "type": "output"
            },
            {
                "filename": "image2.jpg",
                "subfolder": "",
                "type": "temp"
            }
        ]
    }));

    let multi_urls = client_with_slash.outputs_to_urls(&multi_outputs);
    println!("Generated {} URLs:", multi_urls.len());
    for (i, url) in multi_urls.iter().enumerate() {
        println!("  {}. {}", i + 1, url);
        if url.contains("//view") {
            println!("     ❌ FAILED: URL contains double slash!");
        } else {
            println!("     ✅ PASSED: No double slash found");
        }
    }

    println!("\n🎯 Expected vs Actual URLs:");
    println!("Expected: http://192.168.0.193:8188/view?filename=ComfyUI_02046_.png&subfolder=&type=output");
    if !urls_with_slash.is_empty() {
        println!("Actual:   {}", urls_with_slash[0]);
        if urls_with_slash[0] == "http://192.168.0.193:8188/view?filename=ComfyUI_02046_.png&subfolder=&type=output" {
            println!("✅ URLs match perfectly!");
        } else {
            println!("❌ URLs don't match!");
        }
    }

    println!("\n🔍 Consistency Check:");
    if urls_with_slash == urls_without_slash {
        println!("✅ URLs are consistent regardless of trailing slash in base URL");
    } else {
        println!("❌ URLs are inconsistent!");
        println!("With slash:    {:?}", urls_with_slash);
        println!("Without slash: {:?}", urls_without_slash);
    }

    println!("\n🎉 URL Generation Fix Test Completed!");
    println!("The double slash bug has been fixed. URLs now correctly use single slashes.");
}
