//! Debug script to count resources in test data by service type

use anyhow::Result;
use rag_module::*;
use rag_module::services::search_service::EstateSearchOptions;
use std::collections::HashMap;

#[tokio::main]
async fn main() -> Result<()> {
    println!("🔍 DEBUGGING TEST DATA COUNTS");
    println!("=============================\n");
    
    let rag = create_rag_module("./test_data").await?;
    rag.initialize().await?;
    
    let user_id = "test_user_123";
    
    // Search for ALL resources (no filters)
    let all_results = rag.search_service.search_estate_resources(
        "aws resources", // Generic query to match all
        EstateSearchOptions {
            resource_types: None,
            account_ids: None,
            regions: None,
            services: None,  // NO SERVICE FILTER
            states: None,
            environment: None,
            application: None,
            synced_after: None,
            limit: Some(50), // Get up to 50 resources
            score_threshold: Some(0.001), // Very low threshold to get everything
            include_metadata: true,
            use_anonymous_ids: false,
        },
        None,
        user_id,
    ).await?;
    
    println!("📊 TOTAL RESOURCES FOUND: {}", all_results.len());
    
    // Count by service type
    let mut service_counts: HashMap<String, u32> = HashMap::new();
    let mut resource_type_counts: HashMap<String, u32> = HashMap::new();
    
    for result in &all_results {
        // Count by service
        let service = result.get("service")
            .and_then(|v| v.as_str())
            .unwrap_or("unknown")
            .to_string();
        *service_counts.entry(service).or_insert(0) += 1;
        
        // Count by resource_type
        let resource_type = result.get("resource_type")
            .and_then(|v| v.as_str())
            .unwrap_or("unknown")
            .to_string();
        *resource_type_counts.entry(resource_type).or_insert(0) += 1;
    }
    
    println!("\n📋 BY SERVICE:");
    for (service, count) in service_counts.iter() {
        println!("   {} = {} resources", service, count);
    }
    
    println!("\n📋 BY RESOURCE TYPE:");
    for (resource_type, count) in resource_type_counts.iter() {
        println!("   {} = {} resources", resource_type, count);
    }
    
    // Test specific service searches
    println!("\n🔍 TESTING SPECIFIC SERVICE SEARCHES:");
    
    for service in ["ec2", "rds", "iam", "s3", "lambda"].iter() {
        let service_results = rag.search_service.search_estate_resources(
            &format!("{} resources", service),
            EstateSearchOptions {
                resource_types: None,
                account_ids: None,
                regions: None,
                services: Some(vec![service.to_string()]),
                states: None,
                environment: None,
                application: None,
                synced_after: None,
                limit: Some(20),
                score_threshold: Some(0.001),
                include_metadata: true,
                use_anonymous_ids: false,
            },
            None,
            user_id,
        ).await?;
        
        println!("   {} service filter: {} results", service, service_results.len());
    }
    
    Ok(())
}