//! URL generation tests

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

#[test]
fn test_outputs_to_urls_no_double_slash() {
    // Test with base URL ending with slash
    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
    let mut outputs = HashMap::new();
    outputs.insert("58".to_string(), json!({
        "images": [
            {
                "filename": "ComfyUI_02046_.png",
                "subfolder": "",
                "type": "output"
            }
        ]
    }));

    // Test with base URL ending with slash
    let urls_with_slash = client_with_slash.outputs_to_urls(&outputs);
    assert_eq!(urls_with_slash.len(), 1);
    let url_with_slash = &urls_with_slash[0];
    println!("URL with slash: {}", url_with_slash);
    
    // Should not contain double slash
    assert!(!url_with_slash.contains("//view"), 
        "URL should not contain double slash: {}", url_with_slash);
    assert!(url_with_slash.contains("/view?"), 
        "URL should contain single slash before view: {}", url_with_slash);

    // Test with base URL without ending slash
    let urls_without_slash = client_without_slash.outputs_to_urls(&outputs);
    assert_eq!(urls_without_slash.len(), 1);
    let url_without_slash = &urls_without_slash[0];
    println!("URL without slash: {}", url_without_slash);
    
    // Should not contain double slash
    assert!(!url_without_slash.contains("//view"), 
        "URL should not contain double slash: {}", url_without_slash);
    assert!(url_without_slash.contains("/view?"), 
        "URL should contain single slash before view: {}", url_without_slash);

    // Both URLs should be the same
    assert_eq!(url_with_slash, url_without_slash, 
        "URLs should be identical regardless of trailing slash in base URL");

    // Expected URL format
    let expected_url = "http://192.168.0.193:8188/view?filename=ComfyUI_02046_.png&subfolder=&type=output";
    assert_eq!(url_with_slash, expected_url);
    assert_eq!(url_without_slash, expected_url);
}

#[test]
fn test_outputs_to_urls_multiple_images() {
    let config = ComfyUIClientConfig {
        base_url: "http://localhost:8188/".to_string(),
        ..Default::default()
    };
    let client = HTTPClient::new(config).unwrap();

    let mut outputs = HashMap::new();
    outputs.insert("58".to_string(), json!({
        "images": [
            {
                "filename": "image1.png",
                "subfolder": "temp",
                "type": "output"
            },
            {
                "filename": "image2.jpg",
                "subfolder": "",
                "type": "temp"
            }
        ]
    }));

    let urls = client.outputs_to_urls(&outputs);
    assert_eq!(urls.len(), 2);

    for url in &urls {
        println!("Generated URL: {}", url);
        assert!(!url.contains("//view"), "URL should not contain double slash: {}", url);
        assert!(url.contains("/view?"), "URL should contain single slash before view: {}", url);
        assert!(url.starts_with("http://localhost:8188/view?"), "URL should have correct base");
    }

    // Check specific URLs
    assert!(urls.contains(&"http://localhost:8188/view?filename=image1.png&subfolder=temp&type=output".to_string()));
    assert!(urls.contains(&"http://localhost:8188/view?filename=image2.jpg&subfolder=&type=temp".to_string()));
}

#[test]
fn test_outputs_to_urls_empty_outputs() {
    let config = ComfyUIClientConfig {
        base_url: "http://localhost:8188".to_string(),
        ..Default::default()
    };
    let client = HTTPClient::new(config).unwrap();

    let outputs = HashMap::new();
    let urls = client.outputs_to_urls(&outputs);
    assert_eq!(urls.len(), 0);
}

#[test]
fn test_outputs_to_urls_no_images() {
    let config = ComfyUIClientConfig {
        base_url: "http://localhost:8188".to_string(),
        ..Default::default()
    };
    let client = HTTPClient::new(config).unwrap();

    let mut outputs = HashMap::new();
    outputs.insert("58".to_string(), json!({
        "text": "some text output"
    }));

    let urls = client.outputs_to_urls(&outputs);
    assert_eq!(urls.len(), 0);
}

#[test]
fn test_different_base_url_formats() {
    let test_cases = vec![
        "http://192.168.0.193:8188",
        "http://192.168.0.193:8188/",
        "https://my-comfyui.com",
        "https://my-comfyui.com/",
        "http://localhost:8188",
        "http://localhost:8188/",
    ];

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

    for base_url in test_cases {
        println!("Testing base URL: {}", base_url);
        
        let config = ComfyUIClientConfig {
            base_url: base_url.to_string(),
            ..Default::default()
        };
        let client = HTTPClient::new(config).unwrap();
        
        let urls = client.outputs_to_urls(&outputs);
        assert_eq!(urls.len(), 1);
        
        let url = &urls[0];
        println!("Generated URL: {}", url);
        
        // Should not contain double slash
        assert!(!url.contains("//view"), 
            "URL should not contain double slash for base URL '{}': {}", base_url, url);
        
        // Should contain single slash before view
        assert!(url.contains("/view?"), 
            "URL should contain single slash before view for base URL '{}': {}", base_url, url);
        
        // Should start with the base URL (without trailing slash) + /view
        let expected_base = base_url.trim_end_matches('/');
        assert!(url.starts_with(&format!("{}/view?", expected_base)), 
            "URL should start with correct base for '{}': {}", base_url, url);
    }
}
