//! Debug search in detail to see what's happening with filtering

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

#[tokio::main]
async fn main() -> Result<()> {
    println!("🔍 DETAILED SEARCH DEBUG");
    println!("========================\n");
    
    let rag = create_rag_module("./test_data").await?;
    rag.initialize().await?;
    
    let user_id = "test_user_123";
    
    // Test EC2 search with very low threshold to see all potential matches
    println!("🔍 EC2 SEARCH - No Filters (should see all EC2 matches):");
    let ec2_results = rag.search_service.search_estate_resources(
        "ec2 instances",
        EstateSearchOptions {
            resource_types: None,
            account_ids: None,
            regions: None,
            services: Some(vec!["ec2".to_string()]),
            states: None,
            environment: None,
            application: None,
            synced_after: None,
            limit: Some(10), // Get more results
            score_threshold: Some(0.05), // Very low threshold
            include_metadata: true,
            use_anonymous_ids: false,
        },
        None,
        user_id,
    ).await?;
    
    println!("Found {} EC2 results:", ec2_results.len());
    for (i, result) in ec2_results.iter().enumerate() {
        let id = result.get("id").and_then(|v| v.as_str()).unwrap_or("unknown");
        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 instance_id = result.get("instance_id").and_then(|v| v.as_str()).unwrap_or("none");
        let score = result.get("score").and_then(|v| v.as_f64()).unwrap_or(0.0);
        
        println!("  {}. ID: {}", i + 1, id);
        println!("     Service: {}, Type: {}", service, resource_type);
        println!("     Instance ID: {}", instance_id);
        println!("     Score: {:.4}", score);
        println!();
    }
    
    println!("{}", "=".repeat(60));
    
    // Test RDS search
    println!("🔍 RDS SEARCH:");
    let rds_results = rag.search_service.search_estate_resources(
        "rds databases",
        EstateSearchOptions {
            resource_types: None,
            account_ids: None,
            regions: None,
            services: Some(vec!["rds".to_string()]),
            states: None,
            environment: None,
            application: None,
            synced_after: None,
            limit: Some(10),
            score_threshold: Some(0.05),
            include_metadata: true,
            use_anonymous_ids: false,
        },
        None,
        user_id,
    ).await?;
    
    println!("Found {} RDS results:", rds_results.len());
    for (i, result) in rds_results.iter().enumerate() {
        let id = result.get("id").and_then(|v| v.as_str()).unwrap_or("unknown");
        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 db_id = result.get("db_instance_identifier").and_then(|v| v.as_str()).unwrap_or("none");
        let score = result.get("score").and_then(|v| v.as_f64()).unwrap_or(0.0);
        
        println!("  {}. ID: {}", i + 1, id);
        println!("     Service: {}, Type: {}", service, resource_type);
        println!("     DB ID: {}", db_id);
        println!("     Score: {:.4}", score);
        println!();
    }
    
    println!("{}", "=".repeat(60));
    
    // Test IAM search
    println!("🔍 IAM SEARCH:");
    let iam_results = rag.search_service.search_estate_resources(
        "iam users roles",
        EstateSearchOptions {
            resource_types: None,
            account_ids: None,
            regions: None,
            services: Some(vec!["iam".to_string()]),
            states: None,
            environment: None,
            application: None,
            synced_after: None,
            limit: Some(10),
            score_threshold: Some(0.05),
            include_metadata: true,
            use_anonymous_ids: false,
        },
        None,
        user_id,
    ).await?;
    
    println!("Found {} IAM results:", iam_results.len());
    for (i, result) in iam_results.iter().enumerate() {
        let id = result.get("id").and_then(|v| v.as_str()).unwrap_or("unknown");
        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 user_name = result.get("user_name").and_then(|v| v.as_str()).unwrap_or("none");
        let role_name = result.get("role_name").and_then(|v| v.as_str()).unwrap_or("none");
        let score = result.get("score").and_then(|v| v.as_f64()).unwrap_or(0.0);
        
        println!("  {}. ID: {}", i + 1, id);
        println!("     Service: {}, Type: {}", service, resource_type);
        println!("     User: {}, Role: {}", user_name, role_name);
        println!("     Score: {:.4}", score);
        println!();
    }
    
    Ok(())
}