//! Debug what's actually stored in estate collection

use anyhow::Result;
use rag_module::*;

#[tokio::main]
async fn main() -> Result<()> {
    println!("🔍 Debug: Estate Collection Contents");
    println!("===================================");

    let base_path = std::env::current_dir()?.join("working-demo");  // Same path as your working demo
    let rag = create_rag_module(base_path).await?;
    rag.initialize().await?;

    // Check what's in the estate collection
    let user_id = "demo_user";  // Same user_id from working demo
    
    println!("📂 Checking estate documents for user: {}", user_id);
    let docs = rag.get_collection_documents("aws_estate", user_id).await?;
    
    println!("📊 Total documents found: {}", docs.len());
    
    for (i, doc) in docs.iter().enumerate() {
        println!("\n📄 Document {}:", i + 1);
        println!("  ID: {}", doc.id);
        println!("  Content length: {} chars", doc.content.len());
        println!("  Embedding dimensions: {}", doc.embedding.len());
        println!("  Content preview: {}...", &doc.content[..std::cmp::min(100, doc.content.len())]);
        
        // Check if content looks encrypted
        if doc.content.starts_with("ey") || doc.content.len() > 200 {
            println!("  ✅ Content appears encrypted (base64)");
        } else {
            println!("  ❌ Content might not be encrypted");
        }
        
        // Check embedding stats
        if doc.embedding.len() == 1024 {
            println!("  ✅ Has BGE-M3 embedding (1024D)");
            let avg = doc.embedding.iter().sum::<f32>() / doc.embedding.len() as f32;
            println!("  📊 Embedding avg: {:.6}", avg);
        } else if doc.embedding.len() == 1 {
            println!("  ⚠️  Has dummy embedding (1D): {:?}", doc.embedding);
        } else {
            println!("  ❓ Unexpected embedding size: {}D", doc.embedding.len());
        }
    }

    if docs.is_empty() {
        println!("\n❌ NO DOCUMENTS FOUND!");
        println!("This explains why vector search returns 0 results.");
        println!("You need to add some estate data first.");
    } else {
        println!("\n✅ Documents exist - the issue might be:");
        println!("  1. Similarity threshold too high (try 0.1 or 0.0)");
        println!("  2. Vector index not updated");
        println!("  3. Embedding mismatch between query and docs");
    }

    Ok(())
}