//! Test vector search with manual data

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

#[tokio::main]
async fn main() -> Result<()> {
    println!("🧪 Test Vector Search with Fresh Data");
    println!("=====================================");

    // Use a fresh directory to avoid conflicts
    let base_path = std::env::current_dir()?.join("test-vector-search");
    let rag = create_rag_module(base_path).await?;
    rag.initialize().await?;

    let user_id = "test_user";
    
    println!("📝 Step 1: Add RDS data that should match 'List my RDS instances'");
    
    // Add RDS-specific data
    let rds_data = json!([{
        "account_id": "123456789012",
        "account_name": "Production Account",
        "services": {
            "rds": {
                "instances": [
                    {
                        "db_instance_identifier": "prod-mysql-main",
                        "db_name": "production_db",
                        "engine": "mysql",
                        "engine_version": "8.0.35",
                        "db_instance_class": "db.t3.medium",
                        "description": "Main production MySQL database for user data and transactions"
                    },
                    {
                        "db_instance_identifier": "analytics-postgres",
                        "db_name": "analytics",
                        "engine": "postgres", 
                        "engine_version": "15.4",
                        "db_instance_class": "db.r5.large",
                        "description": "PostgreSQL database for analytics and reporting queries"
                    }
                ]
            }
        }
    }]);

    let doc_ids = rag.process_aws_estate(rds_data, user_id).await?;
    println!("✅ Added {} RDS documents", doc_ids.len());

    // Wait for processing
    println!("⏳ Waiting 2 seconds for processing...");
    tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;

    // Verify documents exist
    let docs = rag.get_collection_documents("aws_estate", user_id).await?;
    println!("📊 Total documents stored: {}", docs.len());
    
    for (i, doc) in docs.iter().enumerate() {
        println!("  Doc {}: {} chars, {}D embedding", i+1, doc.content.len(), doc.embedding.len());
    }

    if docs.is_empty() {
        println!("❌ No documents found - this explains the 0 results");
        return Ok(());
    }

    println!("\n🔍 Step 2: Test vector search with very low threshold");
    
    let search_options = EstateSearchOptions {
        resource_types: None,
        account_ids: None, 
        regions: None,
        services: None,
        states: None,
        environment: None,
        application: None,
        synced_after: None,
        limit: Some(10),
        score_threshold: Some(0.0), // Accept any match
        include_metadata: true,
        use_anonymous_ids: false,
    };

    let results = rag.search_service
        .search_estate_resources("RDS database instances", search_options, None)
        .await?;

    println!("✅ Search results: {} found", results.len());
    
    for (i, result) in results.iter().enumerate() {
        if let Some(score) = result.get("score").and_then(|s| s.as_f64()) {
            if let Some(service) = result.get("service").and_then(|s| s.as_str()) {
                println!("  {}: {} (score: {:.4})", i+1, service, score);
            }
        }
    }

    if results.is_empty() {
        println!("\n🔍 Step 3: Try even more basic search");
        let basic_results = rag.search_service
            .search_estate_resources("database", EstateSearchOptions {
                score_threshold: Some(-1.0), // Accept everything
                limit: Some(5),
                ..Default::default()
            }, None)
            .await?;
        
        println!("Basic search results: {}", basic_results.len());
    }

    Ok(())
}