//! Debug script to examine the "unknown" resources specifically

use anyhow::Result;
use rag_module::*;
use rag_module::services::search_service::EstateSearchOptions;

#[tokio::main]
async fn main() -> Result<()> {
    println!("🔍 DEBUGGING UNKNOWN RESOURCES");
    println!("==============================\n");
    
    let rag = create_rag_module("./test_data").await?;
    rag.initialize().await?;
    
    let user_id = "test_user_123";
    
    // Search for all resources with very low threshold
    let all_results = rag.search_service.search_estate_resources(
        "aws resources",
        EstateSearchOptions {
            resource_types: None,
            account_ids: None,
            regions: None,
            services: None,  
            states: None,
            environment: None,
            application: None,
            synced_after: None,
            limit: Some(20), // Get all resources  
            score_threshold: Some(0.001), 
            include_metadata: true,
            use_anonymous_ids: false,
        },
        None,
        user_id,
    ).await?;
    
    // Separate known vs unknown services
    let mut known_services = Vec::new();
    let mut unknown_services = Vec::new();
    
    for result in &all_results {
        let service = result.get("service")
            .and_then(|v| v.as_str())
            .unwrap_or("unknown");
            
        if service == "unknown" {
            unknown_services.push(result);
        } else {
            known_services.push(result);
        }
    }
    
    println!("📊 RESULTS BREAKDOWN:");
    println!("   Known services: {}", known_services.len());
    println!("   Unknown services: {}", unknown_services.len());
    
    println!("\n✅ KNOWN SERVICES:");
    for (i, result) in known_services.iter().enumerate() {
        let service = result.get("service").and_then(|v| v.as_str()).unwrap_or("unknown");
        let resource_type = result.get("resource_type").and_then(|v| v.as_str()).unwrap_or("unknown");
        let id = result.get("id").and_then(|v| v.as_str()).unwrap_or("unknown");
        println!("   {}. {} / {} ({})", i + 1, service, resource_type, id);
    }
    
    println!("\n❓ UNKNOWN SERVICES - First 5:");
    for (i, result) in unknown_services.iter().take(5).enumerate() {
        let resource_type = result.get("resource_type").and_then(|v| v.as_str()).unwrap_or("unknown");
        let id = result.get("id").and_then(|v| v.as_str()).unwrap_or("unknown");
        
        println!("\n   {}. ID: {}", i + 1, id);
        println!("      Resource Type: {}", resource_type);
        
        // Show all available fields for debugging
        if let Some(obj) = result.as_object() {
            println!("      All fields:");
            for (key, value) in obj.iter() {
                if key != "metadata" { // Skip metadata to avoid clutter
                    let val_str = match value {
                        serde_json::Value::String(s) => s.clone(),
                        serde_json::Value::Number(n) => n.to_string(),
                        serde_json::Value::Bool(b) => b.to_string(),
                        serde_json::Value::Null => "null".to_string(),
                        _ => format!("{:?}", value),
                    };
                    println!("         {}: {}", key, val_str);
                }
            }
        }
    }
    
    Ok(())
}