//! Debug encryption keys and manually decrypt stored data

use anyhow::Result;
use rag_module::*;
use serde_json::Value;
use std::collections::HashMap;

#[tokio::main]
async fn main() -> Result<()> {
    println!("🔍 DEBUGGING ENCRYPTION KEYS & STORED DATA");
    println!("==========================================\n");
    
    // Create RAG module to get encryption service
    let rag = create_rag_module("./test_data").await?;
    rag.initialize().await?;
    
    println!("📋 STEP 1: Reading Raw Stored Data");
    println!("==================================");
    
    // Read the raw documents file
    let docs_file = std::path::Path::new("test_data/qdrant-data/test_user_123/aws_estate-documents.json");
    if docs_file.exists() {
        let content = std::fs::read_to_string(docs_file)?;
        let docs_data: Value = serde_json::from_str(&content)?;
        
        if let Some(documents) = docs_data.get("documents").and_then(|d| d.as_array()) {
            println!("   📊 Total documents in file: {}", documents.len());
            
            // Examine first 3 documents
            for (i, doc) in documents.iter().take(3).enumerate() {
                println!("\n   📄 Document {} Structure:", i + 1);
                println!("      ID: {}", doc.get("id").and_then(|v| v.as_str()).unwrap_or("N/A"));
                
                if let Some(metadata) = doc.get("metadata") {
                    if let Some(obj) = metadata.as_object() {
                        println!("      Metadata keys: {:?}", obj.keys().collect::<Vec<_>>());
                        
                        // Check if encrypted metadata exists
                        if let Some(encrypted_metadata) = obj.get("_encrypted_metadata") {
                            if let Some(encrypted_str) = encrypted_metadata.as_str() {
                                println!("      Encrypted metadata length: {} chars", encrypted_str.len());
                                println!("      First 100 chars: {}", &encrypted_str[..std::cmp::min(100, encrypted_str.len())]);
                            }
                        }
                    }
                }
                
                // Check content encryption
                if let Some(content) = doc.get("content") {
                    if let Some(content_str) = content.as_str() {
                        println!("      Encrypted content length: {} chars", content_str.len());
                        println!("      Content starts with: {}", &content_str[..std::cmp::min(50, content_str.len())]);
                    }
                }
            }
        }
    } else {
        println!("   ❌ Documents file not found!");
        return Ok(());
    }
    
    println!("\n📋 STEP 2: Manual Decryption Test");
    println!("=================================");
    
    // Get the collection documents using RAG's method
    let collection_docs = rag.get_collection_documents("aws_estate", "test_user_123").await?;
    println!("   📊 Documents returned by get_collection_documents: {}", collection_docs.len());
    
    if let Some(first_doc) = collection_docs.first() {
        println!("\n   🔓 Testing decryption on first document:");
        println!("      Document ID: {}", first_doc.id);
        
        // Try to access the encryption service through the RAG module
        // We'll need to check if we can decrypt manually
        
        // Check what's in the document metadata
        println!("      Encrypted metadata length: {} chars", first_doc.metadata.encrypted_metadata.len());
        println!("      Encrypted metadata preview: {}", &first_doc.metadata.encrypted_metadata[..std::cmp::min(100, first_doc.metadata.encrypted_metadata.len())]);
        
        println!("      Created at: {:?}", first_doc.metadata.created_at);
        println!("      Updated at: {:?}", first_doc.metadata.updated_at);
        
        println!("\n   🔍 Checking document content:");
        println!("      Content length: {} chars", first_doc.content.len());
        println!("      Content preview: {}", &first_doc.content[..std::cmp::min(100, first_doc.content.len())]);
    }
    
    println!("\n📋 STEP 3: Search Process Decryption Test");
    println!("========================================");
    
    // Now test what happens during search
    let search_options = rag_module::services::search_service::EstateSearchOptions {
        resource_types: None,
        account_ids: None,
        regions: None,
        services: None,
        states: None,
        environment: None,
        application: None,
        synced_after: None,
        limit: Some(1),
        score_threshold: Some(0.001),
        include_metadata: true,
        use_anonymous_ids: false,
    };
    
    let search_results = rag.search_service.search_estate_resources(
        "aws resource test",
        search_options,
        None,
        "test_user_123",
    ).await?;
    
    if let Some(result) = search_results.first() {
        println!("   ✅ Search returned result:");
        println!("   📄 Result fields:");
        
        for field in ["id", "service", "resource_type", "account_id", "region", "tags"] {
            let value = result.get(field);
            println!("      {}: {:?}", field, value);
        }
        
        println!("\n   🔍 Full result structure:");
        println!("{}", serde_json::to_string_pretty(result)?);
        
    } else {
        println!("   ❌ No search results returned!");
    }
    
    println!("\n📋 STEP 4: Comparison Analysis");
    println!("==============================");
    
    // Compare what we expect vs what we get
    println!("   🎯 Expected: service, resource_type, account_id should have values");
    println!("   🔍 Actual: Check above search results");
    
    if collection_docs.len() != search_results.len() {
        println!("   ⚠️  MISMATCH: Collection has {} docs, search returned {}", 
                collection_docs.len(), search_results.len());
    }
    
    println!("\n🏁 CONCLUSION:");
    println!("   - Check if encrypted content lengths look reasonable");
    println!("   - Verify if search results have proper field values");
    println!("   - Compare raw metadata vs processed search results");
    
    Ok(())
}