//! Debug script to examine the exact metadata structure after decryption

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

#[tokio::main]
async fn main() -> Result<()> {
    println!("🔍 DEBUGGING METADATA STRUCTURE AFTER DECRYPTION");
    println!("===============================================\n");
    
    let rag = create_rag_module("./test_data").await?;
    rag.initialize().await?;
    
    let user_id = "test_user_123";
    
    // Search for just 1 resource to debug its structure
    let results = rag.search_service.search_estate_resources(
        "aws resources",
        EstateSearchOptions {
            resource_types: None,
            account_ids: None,
            regions: None,
            services: None,  
            states: None,
            environment: None,
            application: None,
            synced_after: None,
            limit: Some(1), // Just get 1 for debugging
            score_threshold: Some(0.001), 
            include_metadata: true,
            use_anonymous_ids: false,
        },
        None,
        user_id,
    ).await?;
    
    if let Some(result) = results.first() {
        println!("📋 SINGLE RESULT STRUCTURE:");
        println!("==========================");
        println!("{}", serde_json::to_string_pretty(result)?);
        
        println!("\n📋 TOP-LEVEL KEYS:");
        if let Some(obj) = result.as_object() {
            for key in obj.keys() {
                println!("   - {}", key);
            }
        }
        
        // Check metadata structure specifically  
        if let Some(metadata) = result.get("metadata") {
            println!("\n📋 METADATA KEYS:");
            if let Some(meta_obj) = metadata.as_object() {
                for key in meta_obj.keys() {
                    println!("   - metadata.{}", key);
                }
                
                // Check if there's nested metadata
                if let Some(nested_metadata) = meta_obj.get("metadata") {
                    println!("\n📋 NESTED METADATA KEYS:");
                    if let Some(nested_obj) = nested_metadata.as_object() {
                        for key in nested_obj.keys() {
                            println!("   - metadata.metadata.{}", key);
                        }
                    }
                }
            }
        }
    } else {
        println!("❌ No results found!");
    }
    
    Ok(())
}