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

#[tokio::main]
async fn main() -> Result<()> {
    println!("🔧 Testing Fixed Collection Health and Search");
    
    // Test with existing estate data
    let mut estate_rag = RagModule::new("./example-enhanced-estate-data").await?;
    estate_rag.initialize().await?;
    estate_rag.set_user_context("estate_user_example").await?;
    
    println!("✅ RAG Module initialized");
    
    // Check collection health (should now show real point counts)
    let health = estate_rag.collection_manager.get_collections_health().await?;
    println!("📊 Collection Health (Fixed):");
    println!("   Chat Collection: {} points", health.chat_history.points_count);
    println!("   Estate Collection: {} points", health.aws_estate.points_count);
    
    // Test estate search with very low threshold
    let search_options = EstateSearchOptions {
        resource_types: None,
        account_ids: None,
        regions: Some(vec!["us-east-1".to_string()]),
        services: None,
        states: None,
        environment: None,
        application: None,
        synced_after: None,
        limit: Some(10),
        score_threshold: Some(0.05), // Very low threshold
        include_metadata: true,
        use_anonymous_ids: false,
    };
    
    println!("\n🔍 Testing estate search with low threshold...");
    let results = estate_rag.search_service
        .search_estate_resources("AWS EC2 instances", search_options, None)
        .await?;
    
    println!("✅ Estate Search Results: {}", results.len());
    for (i, result) in results.iter().take(5).enumerate() {
        if let (Some(resource_type), Some(region), Some(account)) = (
            result.get("resource_type").and_then(|v| v.as_str()),
            result.get("region").and_then(|v| v.as_str()),
            result.get("account_id").and_then(|v| v.as_str()),
        ) {
            let score = result.get("score").and_then(|v| v.as_f64()).unwrap_or(0.0);
            println!("   {}. {} in {} ({}) - Score: {:.4}", 
                i + 1, resource_type, region, account, score);
        }
    }
    
    Ok(())
}