use anyhow::Result;
use rag_module::RagModule;
use rag_module::services::search_service::EstateSearchOptions;
use rag_module::types::Document;
use serde_json::json;
use std::collections::HashMap;
use indexmap::IndexMap;

#[tokio::main]
async fn main() -> Result<()> {
    println!("🏢 AWS Estate Data Example: Embedded vs Server Mode");
    println!("This example works with BOTH Qdrant embedded and server modes!\\n");

    // // Detect which mode we're running in
    // let mode = if std::env::var("QDRANT_URL").is_ok() {
    //     "🌐 Server Mode"
    // } else {
    //     "💾 Embedded Mode"
    // };
    // println!("Running in: {}", mode);
    // println!("─────────────────────────────────────────────────────────");

    // if mode == "💾 Embedded Mode" {
    //     println!("📝 To switch to server mode:");
    //     println!("   1. Run: docker-compose up -d");
    //     println!("   2. Set: export QDRANT_URL=\\\"http://localhost:6334\\\"");
    //     println!("   3. Run this example again");
    // } else {
    //     println!("✅ Using Qdrant server");
    //     println!("   To switch back to embedded mode: unset QDRANT_URL");
    // }
    // println!("─────────────────────────────────────────────────────────\\n");

    // Initialize RAG module with enhanced services
    // The same code works for both modes!
    let mut rag_module = RagModule::new("./example-enhanced-estate-data").await?;
    rag_module.initialize().await?;

    // Set user context for document operations
    let user_id = "estate_user_example";
    rag_module.set_user_context(user_id).await?;

    println!("✅ RAG Module initialized:");
    println!("   - Chat Collection: 1D dummy vectors for scroll-only search");
    println!("   - Estate Collection: 1024D BGE-M3 vectors for semantic search");
    println!("   - Encryption Service: OS keychain integration");
    println!("   - Search Service: Collection-specific algorithms");
    println!("   - Folder Structure: 100% same in both modes!");
    println!();

    // Sample AWS estate data
    let aws_estate_data = json!([
  {
    "account_id": "288761761556",
    "role_arn": null,
    "account_name": "",
    "scan_timestamp": null,
    "services": {
      "ec2": {
        "instances": [
          {
            "instance_id": "i-08619fa3385e63bab",
            "instance_type": "c4.large",
            "state": "running",
            "region": "us-east-1",
            "launch_time": "2025-08-26T12:37:51+00:00",
            "name": "escher-scan-stream-v2"
          },
          {
            "instance_id": "i-0f6ce804a1a464e1d",
            "instance_type": "c4.large",
            "state": "running",
            "region": "us-east-1",
            "launch_time": "2025-08-23T06:45:12+00:00",
            "name": "slack-ec2-stream"
          },
          {
            "instance_id": "i-01697357246074533",
            "instance_type": "t2.micro",
            "state": "running",
            "region": "us-east-1",
            "launch_time": "2025-09-05T07:06:47+00:00",
            "name": "space-argo-dev-inst-1"
          },
          {
            "instance_id": "i-0b1e8b61d9b846a3e",
            "instance_type": "t2.micro",
            "state": "running",
            "region": "us-east-1",
            "launch_time": "2025-07-21T04:22:19+00:00",
            "name": "escher-ec2"
          },
          {
            "instance_id": "i-0a5ff673a52c7a1c7",
            "instance_type": "c4.large",
            "state": "running",
            "region": "us-east-1",
            "launch_time": "2025-07-28T05:05:24+00:00",
            "name": "escher-stream-ec2"
          },
          {
            "instance_id": "i-06a446d8aaec15d27",
            "instance_type": "c4.large",
            "state": "running",
            "region": "us-east-1",
            "launch_time": "2025-08-29T04:11:05+00:00",
            "name": "finops-ec2-stream"
          },
          {
            "instance_id": "i-07901ab76d1aca487",
            "instance_type": "c1.xlarge",
            "state": "running",
            "region": "us-east-1",
            "launch_time": "2025-09-29T10:42:10+00:00",
            "name": "dev-benchmark"
          }
        ],
        "volumes_count": 13,
        "elastic_ips_total": 7,
        "elastic_ips_attached": 3,
        "elastic_ips_unattached": 4,
        "elastic_ips_instance_attached": 0,
        "elastic_ips_eni_attached": 3,
        "vpcs_count": 7,
        "permissions": {
          "service_info": {
            "display_name": "EC2 Compute",
            "icon": "🖥️",
            "category": "compute",
            "description": "Amazon Elastic Compute Cloud"
          },
          "actions": {
            "permission_level": "Full",
            "create": {
              "allowed": true,
              "display_name": "Create",
              "risk_level": "medium",
              "aws_actions": [
                "ec2:RunInstances",
                "ec2:CreateTags",
                "ec2:CreateSecurityGroup"
              ],
              "conditions": null
            },
            "start": {
              "allowed": true,
              "display_name": "Start",
              "risk_level": "low",
              "aws_actions": [
                "ec2:StartInstances"
              ],
              "conditions": null
            },
            "stop": {
              "allowed": true,
              "display_name": "Stop",
              "risk_level": "low",
              "aws_actions": [
                "ec2:StopInstances"
              ],
              "conditions": null
            },
            "terminate": {
              "allowed": true,
              "display_name": "Terminate",
              "risk_level": "high",
              "aws_actions": [
                "ec2:TerminateInstances"
              ],
              "conditions": null
            },
            "read": {
              "allowed": true,
              "display_name": "Read",
              "risk_level": "low",
              "aws_actions": [
                "ec2:DescribeInstances",
                "ec2:DescribeInstanceStatus"
              ],
              "conditions": null
            }
          }
        }
      },
      "ebs": {
        "total_volumes": 13,
        "attached_volumes": 8,
        "unattached_volumes": 5,
        "recently_detached": 0,
        "long_term_unused": 5,
        "total_size_gb": 1425,
        "attached_size_gb": 949,
        "unattached_size_gb": 476,
        "long_term_unused_size_gb": 476,
        "volume_types": {
          "gp2": {
            "count": 7,
            "unattached": 3
          },
          "gp3": {
            "count": 6,
            "unattached": 2
          }
        },
        "regions_with_unused": [
          "us-east-1"
        ],
        "region_unused_counts": {
          "us-east-1": 5
        }
      },
      "rds": {
        "instances": [
          {
            "instance_id": "db-VXDRDM3CQ6OIKGF7HLGVPPO454",
            "db_instance_identifier": "dev-eshop-mysql-rds",
            "db_instance_class": "db.t3.micro",
            "engine": "mysql",
            "db_instance_status": "available",
            "region": "us-east-1",
            "name": "dev-eshop-mysql-rds",
            "replication_role": "Instance",
            "replication_source": null
          },
          {
            "instance_id": "db-2XYHZFX3PDXT3CD3LGIK435ALY",
            "db_instance_identifier": "dev-tesserade-pg-rds",
            "db_instance_class": "db.m5.xlarge",
            "engine": "postgres",
            "db_instance_status": "available",
            "region": "us-east-1",
            "name": "dev-tesserade-pg-rds",
            "replication_role": "Instance",
            "replication_source": null
          },
          {
            "instance_id": "db-QTKHKSX6HCJBUAIZSSTOLAHQMY",
            "db_instance_identifier": "mysql-test-instance1",
            "db_instance_class": "db.t3.micro",
            "engine": "mysql",
            "db_instance_status": "available",
            "region": "us-east-1",
            "name": "mysql-test-instance1",
            "replication_role": "Instance",
            "replication_source": null
          },
          {
            "instance_id": "db-AO7N2SOEFDAE2NN4VBLPDRKGHM",
            "db_instance_identifier": "pg-instance-main1",
            "db_instance_class": "db.t3.small",
            "engine": "postgres",
            "db_instance_status": "available",
            "region": "us-east-1",
            "name": "pg-instance-main1",
            "replication_role": "Instance",
            "replication_source": null
          },
          {
            "instance_id": "db-ZM5OP7TCL4CTFCJ67SL5ORIDPQ",
            "db_instance_identifier": "pg-instance-main2",
            "db_instance_class": "db.t4g.medium",
            "engine": "postgres",
            "db_instance_status": "available",
            "region": "us-east-1",
            "name": "pg-instance-main2",
            "replication_role": "Instance",
            "replication_source": null
          },
          {
            "instance_id": "db-QUIUOU5G7GDUMLPJRSPRID4UYU",
            "db_instance_identifier": "prod-ecomm-mysql-rds-instance-1",
            "db_instance_class": "db.r5.xlarge",
            "engine": "aurora-mysql",
            "db_instance_status": "available",
            "region": "us-east-1",
            "name": "prod-ecomm-mysql-rds-instance-1",
            "replication_role": "Instance",
            "replication_source": null
          },
          {
            "instance_id": "db-2FJQN5PFCZJSGY3DGLP3QAFDAA",
            "db_instance_identifier": "prod-tesserade-trademan-pg-rds",
            "db_instance_class": "db.t3.micro",
            "engine": "postgres",
            "db_instance_status": "available",
            "region": "us-east-1",
            "name": "prod-tesserade-trademan-pg-rds",
            "replication_role": "Instance",
            "replication_source": null
          }
        ],
        "clusters_count": 1,
        "snapshots_count": 31,
        "permissions": {
          "service_info": {
            "display_name": "RDS Database",
            "icon": "🗄️",
            "category": "database",
            "description": "Amazon Relational Database Service"
          },
          "actions": {
            "permission_level": "Full",
            "create": {
              "allowed": true,
              "display_name": "Create",
              "risk_level": "medium",
              "aws_actions": [
                "rds:CreateDBInstance",
                "rds:CreateDBCluster"
              ],
              "conditions": null
            },
            "read": {
              "allowed": true,
              "display_name": "Read",
              "risk_level": "low",
              "aws_actions": [
                "rds:DescribeDBInstances",
                "rds:DescribeDBClusters"
              ],
              "conditions": null
            },
            "modify": {
              "allowed": true,
              "display_name": "Modify",
              "risk_level": "medium",
              "aws_actions": [
                "rds:ModifyDBInstance",
                "rds:ModifyDBCluster"
              ],
              "conditions": null
            },
            "delete": {
              "allowed": true,
              "display_name": "Delete",
              "risk_level": "high",
              "aws_actions": [
                "rds:DeleteDBInstance",
                "rds:DeleteDBCluster"
              ],
              "conditions": null
            }
          }
        }
      },
      "s3": {
        "total_buckets": 23,
        "public_buckets": 0,
        "bucket_names": [
          "escher-bucket-test-dev (us-east-1)",
          "escher-dev-test (us-east-1)",
          "test-escher-prod (us-east-1)",
          "test-escher (us-east-1)",
          "rag-client-backup-20250925-224157 (us-east-1)",
          "escher-dev-cf-templates-1758030103 (ap-south-1)",
          "escher-updates-frontend-dev (ap-southeast-1)",
          "escher-app-updates (ap-southeast-1)",
          "escher-test (us-east-1)",
          "aws-sam-cli-managed-default-samclisourcebucket-psntlsjjdvyv (ap-south-2)",
          "escher-dev-cf-templates-1756985923 (ap-south-1)",
          "aws-sam-cli-managed-default-samclisourcebucket-9wpmsdrbseyf (ap-south-1)",
          "aws-sam-cli-managed-default-samclisourcebucket-4wkburapvnib (us-west-2)",
          "test-bucket-cada367e-0484-4967-b820-27b24a9a8390 (us-east-1)",
          "test-bucket-5f562756-9661-4a54-ab9d-718afd1857e7 (us-east-1)",
          "test-bucket-bb401105-5ede-427f-bb34-6d46cfba0ddf (us-east-1)",
          "stackset-escher-app-infra-pipelinebuiltartifactbuc-hustcpjpo3rz (us-west-1)",
          "stackset-escher-app-infra-pipelinebuiltartifactbuc-encqypul4oqc (us-west-1)",
          "cdc-data-pg-instance-main2 (us-east-1)",
          "aws-sam-cli-managed-default-samclisourcebucket-3mhsgjvm6ikb (us-east-1)"
        ],
        "latest_buckets_info": [
          {
            "name": "escher-bucket-test-dev",
            "s3_identifier": "escher-bucket-test-dev",
            "creation_date": "2025-10-03T05:05:37+00:00",
            "region": "us-east-1",
            "is_public": false
          },
          {
            "name": "escher-dev-test",
            "s3_identifier": "escher-dev-test",
            "creation_date": "2025-10-01T13:46:57+00:00",
            "region": "us-east-1",
            "is_public": false
          },
          {
            "name": "test-escher-prod",
            "s3_identifier": "test-escher-prod",
            "creation_date": "2025-10-01T08:44:14+00:00",
            "region": "us-east-1",
            "is_public": false
          },
          {
            "name": "test-escher",
            "s3_identifier": "test-escher",
            "creation_date": "2025-09-30T13:20:59+00:00",
            "region": "us-east-1",
            "is_public": false
          },
          {
            "name": "rag-client-backup-20250925-224157",
            "s3_identifier": "rag-client-backup-20250925-224157",
            "creation_date": "2025-09-25T17:12:00+00:00",
            "region": "us-east-1",
            "is_public": false
          },
          {
            "name": "escher-dev-cf-templates-1758030103",
            "s3_identifier": "escher-dev-cf-templates-1758030103",
            "creation_date": "2025-09-16T13:41:58+00:00",
            "region": "ap-south-1",
            "is_public": false
          },
          {
            "name": "escher-updates-frontend-dev",
            "s3_identifier": "escher-updates-frontend-dev",
            "creation_date": "2025-09-15T09:31:14+00:00",
            "region": "ap-southeast-1",
            "is_public": false
          },
          {
            "name": "escher-app-updates",
            "s3_identifier": "escher-app-updates",
            "creation_date": "2025-09-12T09:43:41+00:00",
            "region": "ap-southeast-1",
            "is_public": false
          },
          {
            "name": "escher-test",
            "s3_identifier": "escher-test",
            "creation_date": "2025-09-12T07:19:20+00:00",
            "region": "us-east-1",
            "is_public": false
          },
          {
            "name": "aws-sam-cli-managed-default-samclisourcebucket-psntlsjjdvyv",
            "s3_identifier": "aws-sam-cli-managed-default-samclisourcebucket-psntlsjjdvyv",
            "creation_date": "2025-09-06T09:23:18+00:00",
            "region": "ap-south-2",
            "is_public": false
          },
          {
            "name": "escher-dev-cf-templates-1756985923",
            "s3_identifier": "escher-dev-cf-templates-1756985923",
            "creation_date": "2025-09-04T11:38:44+00:00",
            "region": "ap-south-1",
            "is_public": false
          },
          {
            "name": "aws-sam-cli-managed-default-samclisourcebucket-9wpmsdrbseyf",
            "s3_identifier": "aws-sam-cli-managed-default-samclisourcebucket-9wpmsdrbseyf",
            "creation_date": "2025-08-06T03:24:39+00:00",
            "region": "ap-south-1",
            "is_public": false
          },
          {
            "name": "aws-sam-cli-managed-default-samclisourcebucket-4wkburapvnib",
            "s3_identifier": "aws-sam-cli-managed-default-samclisourcebucket-4wkburapvnib",
            "creation_date": "2025-08-06T03:22:33+00:00",
            "region": "us-west-2",
            "is_public": false
          },
          {
            "name": "test-bucket-cada367e-0484-4967-b820-27b24a9a8390",
            "s3_identifier": "test-bucket-cada367e-0484-4967-b820-27b24a9a8390",
            "creation_date": "2025-07-30T09:14:40+00:00",
            "region": "us-east-1",
            "is_public": false
          },
          {
            "name": "test-bucket-5f562756-9661-4a54-ab9d-718afd1857e7",
            "s3_identifier": "test-bucket-5f562756-9661-4a54-ab9d-718afd1857e7",
            "creation_date": "2025-07-30T09:09:28+00:00",
            "region": "us-east-1",
            "is_public": false
          },
          {
            "name": "test-bucket-bb401105-5ede-427f-bb34-6d46cfba0ddf",
            "s3_identifier": "test-bucket-bb401105-5ede-427f-bb34-6d46cfba0ddf",
            "creation_date": "2025-07-30T06:45:21+00:00",
            "region": "us-east-1",
            "is_public": false
          },
          {
            "name": "stackset-escher-app-infra-pipelinebuiltartifactbuc-hustcpjpo3rz",
            "s3_identifier": "stackset-escher-app-infra-pipelinebuiltartifactbuc-hustcpjpo3rz",
            "creation_date": "2025-07-16T13:02:42+00:00",
            "region": "us-west-1",
            "is_public": false
          },
          {
            "name": "stackset-escher-app-infra-pipelinebuiltartifactbuc-encqypul4oqc",
            "s3_identifier": "stackset-escher-app-infra-pipelinebuiltartifactbuc-encqypul4oqc",
            "creation_date": "2025-06-14T13:03:10+00:00",
            "region": "us-west-1",
            "is_public": false
          },
          {
            "name": "cdc-data-pg-instance-main2",
            "s3_identifier": "cdc-data-pg-instance-main2",
            "creation_date": "2025-06-11T06:33:06+00:00",
            "region": "us-east-1",
            "is_public": false
          },
          {
            "name": "aws-sam-cli-managed-default-samclisourcebucket-3mhsgjvm6ikb",
            "s3_identifier": "aws-sam-cli-managed-default-samclisourcebucket-3mhsgjvm6ikb",
            "creation_date": "2025-05-17T00:55:51+00:00",
            "region": "us-east-1",
            "is_public": false
          }
        ],
        "showing_latest": 20,
        "permissions": {
          "service_info": {
            "display_name": "S3 Storage",
            "icon": "📁",
            "category": "storage",
            "description": "Amazon Simple Storage Service"
          },
          "actions": {
            "permission_level": "Full",
            "read": {
              "allowed": true,
              "display_name": "Read",
              "risk_level": "low",
              "aws_actions": [
                "s3:GetObject",
                "s3:ListBucket",
                "s3:GetObjectVersion"
              ],
              "conditions": null
            },
            "write": {
              "allowed": true,
              "display_name": "Write",
              "risk_level": "medium",
              "aws_actions": [
                "s3:PutObject",
                "s3:PutObjectAcl",
                "s3:CreateBucket"
              ],
              "conditions": null
            },
            "delete": {
              "allowed": true,
              "display_name": "Delete",
              "risk_level": "high",
              "aws_actions": [
                "s3:DeleteObject",
                "s3:DeleteBucket",
                "s3:DeleteObjectVersion"
              ],
              "conditions": null
            }
          }
        }
      },
      "lambda": {
        "functions": [
          {
            "name": "eShopping_User_Simulator",
            "lambda_identifier": "eShopping_User_Simulator",
            "runtime": "python3.13",
            "memory_size": 128,
            "timeout": 3,
            "region": "us-east-1",
            "last_modified": "2025-07-10T13:44:39.000+0000"
          },
          {
            "name": "Tessarade-Dividend-Simulation",
            "lambda_identifier": "Tessarade-Dividend-Simulation",
            "runtime": "python3.13",
            "memory_size": 128,
            "timeout": 3,
            "region": "us-east-1",
            "last_modified": "2025-07-18T16:35:14.000+0000"
          },
          {
            "name": "aws-rds-pg-alert-to-slack",
            "lambda_identifier": "aws-rds-pg-alert-to-slack",
            "runtime": "python3.11",
            "memory_size": 128,
            "timeout": 3,
            "region": "us-east-1",
            "last_modified": "2025-08-05T08:05:46.000+0000"
          },
          {
            "name": "EscherGoService",
            "lambda_identifier": "EscherGoService",
            "runtime": "provided.al2",
            "memory_size": 128,
            "timeout": 120,
            "region": "us-east-1",
            "last_modified": "2025-06-19T10:54:30.000+0000"
          },
          {
            "name": "Tessarade-WatchList-Simulation",
            "lambda_identifier": "Tessarade-WatchList-Simulation",
            "runtime": "python3.13",
            "memory_size": 128,
            "timeout": 3,
            "region": "us-east-1",
            "last_modified": "2025-07-18T17:18:58.000+0000"
          },
          {
            "name": "web-service-lambda",
            "lambda_identifier": "web-service-lambda",
            "runtime": "provided.al2023",
            "memory_size": 512,
            "timeout": 30,
            "region": "us-east-1",
            "last_modified": "2025-05-16T14:21:38.000+0000"
          },
          {
            "name": "CDC-reader",
            "lambda_identifier": "CDC-reader",
            "runtime": "provided.al2023",
            "memory_size": 128,
            "timeout": 3,
            "region": "us-east-1",
            "last_modified": "2025-05-29T08:51:29.000+0000"
          },
          {
            "name": "prod-escher-storage-service",
            "lambda_identifier": "prod-escher-storage-service",
            "runtime": "provided.al2",
            "memory_size": 256,
            "timeout": 30,
            "region": "us-east-1",
            "last_modified": "2025-09-25T11:56:59.000+0000"
          },
          {
            "name": "aws-rds-pg-alerts-to-slack",
            "lambda_identifier": "aws-rds-pg-alerts-to-slack",
            "runtime": "python3.11",
            "memory_size": 128,
            "timeout": 3,
            "region": "us-east-1",
            "last_modified": "2025-10-02T07:11:41.000+0000"
          },
          {
            "name": "Tessarade-Company-Simulation",
            "lambda_identifier": "Tessarade-Company-Simulation",
            "runtime": "python3.13",
            "memory_size": 128,
            "timeout": 30,
            "region": "us-east-1",
            "last_modified": "2025-09-18T09:02:13.000+0000"
          },
          {
            "name": "Tessarade-Trades-Simulation",
            "lambda_identifier": "Tessarade-Trades-Simulation",
            "runtime": "python3.13",
            "memory_size": 128,
            "timeout": 30,
            "region": "us-east-1",
            "last_modified": "2025-09-18T09:04:56.000+0000"
          },
          {
            "name": "Tessarade-Investors-Simulation",
            "lambda_identifier": "Tessarade-Investors-Simulation",
            "runtime": "python3.13",
            "memory_size": 128,
            "timeout": 30,
            "region": "us-east-1",
            "last_modified": "2025-09-18T09:10:06.000+0000"
          },
          {
            "name": "postgres-cdc-function",
            "lambda_identifier": "postgres-cdc-function",
            "runtime": "provided.al2",
            "memory_size": 512,
            "timeout": 480,
            "region": "us-east-1",
            "last_modified": "2025-05-22T05:58:42.000+0000"
          },
          {
            "name": "Tessarade-WatchList-Items-Simulation",
            "lambda_identifier": "Tessarade-WatchList-Items-Simulation",
            "runtime": "python3.13",
            "memory_size": 128,
            "timeout": 3,
            "region": "us-east-1",
            "last_modified": "2025-07-18T17:32:18.000+0000"
          },
          {
            "name": "Tessarade-Wallet-Simulation",
            "lambda_identifier": "Tessarade-Wallet-Simulation",
            "runtime": "python3.13",
            "memory_size": 128,
            "timeout": 30,
            "region": "us-east-1",
            "last_modified": "2025-09-18T09:03:38.000+0000"
          }
        ]
      },
      "vpcs": {
        "total_vpcs": 7,
        "default_vpcs": 4,
        "total_subnets": 28,
        "public_subnets": 22,
        "private_subnets": 6,
        "vpc_regions": [
          "us-east-1",
          "ap-south-2",
          "ap-south-1",
          "us-west-2"
        ],
        "internet_gateways_count": 7,
        "total_eips_in_vpcs": 0,
        "attached_eips_in_vpcs": 0,
        "unattached_eips_total": 4
      }
    }
  },
  {
    "account_id": "547639878546",
    "role_arn": "arn:aws:iam::547639878546:role/escher_cross_role",
    "account_name": "",
    "scan_timestamp": null,
    "services": {
      "ec2": {
        "instances": [
          {
            "instance_id": "i-0f9f85890fc371f78",
            "instance_type": "t2.micro",
            "state": "running",
            "region": "us-east-1",
            "launch_time": "2025-08-22T18:19:11+00:00",
            "name": "i-0f9f85890fc371f78"
          }
        ],
        "volumes_count": 1,
        "elastic_ips_total": 3,
        "elastic_ips_attached": 0,
        "elastic_ips_unattached": 3,
        "elastic_ips_instance_attached": 0,
        "elastic_ips_eni_attached": 0,
        "vpcs_count": 1,
        "permissions": {
          "service_info": {
            "display_name": "EC2 Compute",
            "icon": "🖥️",
            "category": "compute",
            "description": "Amazon Elastic Compute Cloud"
          },
          "actions": {
            "permission_level": "Full",
            "create": {
              "allowed": true,
              "display_name": "Create",
              "risk_level": "medium",
              "aws_actions": [
                "ec2:RunInstances",
                "ec2:CreateTags",
                "ec2:CreateSecurityGroup"
              ],
              "conditions": null
            },
            "start": {
              "allowed": true,
              "display_name": "Start",
              "risk_level": "low",
              "aws_actions": [
                "ec2:StartInstances"
              ],
              "conditions": null
            },
            "stop": {
              "allowed": true,
              "display_name": "Stop",
              "risk_level": "low",
              "aws_actions": [
                "ec2:StopInstances"
              ],
              "conditions": null
            },
            "terminate": {
              "allowed": true,
              "display_name": "Terminate",
              "risk_level": "high",
              "aws_actions": [
                "ec2:TerminateInstances"
              ],
              "conditions": null
            },
            "read": {
              "allowed": true,
              "display_name": "Read",
              "risk_level": "low",
              "aws_actions": [
                "ec2:DescribeInstances",
                "ec2:DescribeInstanceStatus"
              ],
              "conditions": null
            }
          }
        }
      },
      "ebs": {
        "total_volumes": 1,
        "attached_volumes": 1,
        "unattached_volumes": 0,
        "recently_detached": 0,
        "long_term_unused": 0,
        "total_size_gb": 8,
        "attached_size_gb": 8,
        "unattached_size_gb": 0,
        "long_term_unused_size_gb": 0,
        "volume_types": {
          "gp2": {
            "count": 1,
            "unattached": 0
          }
        },
        "regions_with_unused": [],
        "region_unused_counts": {}
      },
      "s3": {
        "total_buckets": 2,
        "public_buckets": 0,
        "bucket_names": [
          "aparna-dev-test (us-east-1)",
          "tex-instru-test-bckt (us-east-1)"
        ],
        "latest_buckets_info": [
          {
            "name": "aparna-dev-test",
            "s3_identifier": "aparna-dev-test",
            "creation_date": "2025-10-03T07:16:28+00:00",
            "region": "us-east-1",
            "is_public": false
          },
          {
            "name": "tex-instru-test-bckt",
            "s3_identifier": "tex-instru-test-bckt",
            "creation_date": "2025-07-24T04:50:07+00:00",
            "region": "us-east-1",
            "is_public": false
          }
        ],
        "showing_latest": 2,
        "permissions": {
          "service_info": {
            "display_name": "S3 Storage",
            "icon": "📁",
            "category": "storage",
            "description": "Amazon Simple Storage Service"
          },
          "actions": {
            "permission_level": "Full",
            "read": {
              "allowed": true,
              "display_name": "Read",
              "risk_level": "low",
              "aws_actions": [
                "s3:GetObject",
                "s3:ListBucket",
                "s3:GetObjectVersion"
              ],
              "conditions": null
            },
            "write": {
              "allowed": true,
              "display_name": "Write",
              "risk_level": "medium",
              "aws_actions": [
                "s3:PutObject",
                "s3:PutObjectAcl",
                "s3:CreateBucket"
              ],
              "conditions": null
            },
            "delete": {
              "allowed": true,
              "display_name": "Delete",
              "risk_level": "high",
              "aws_actions": [
                "s3:DeleteObject",
                "s3:DeleteBucket",
                "s3:DeleteObjectVersion"
              ],
              "conditions": null
            }
          }
        }
      },
      "vpcs": {
        "total_vpcs": 1,
        "default_vpcs": 1,
        "total_subnets": 6,
        "public_subnets": 6,
        "private_subnets": 0,
        "vpc_regions": [
          "us-east-1"
        ],
        "internet_gateways_count": 1,
        "total_eips_in_vpcs": 0,
        "attached_eips_in_vpcs": 0,
        "unattached_eips_total": 3
      }
    }
  }
]);

    // === 1. ENHANCED DOCUMENT PROCESSING ===
    println!("📊 Processing AWS estate data with enhanced document service...");
    
    // Convert AWS estate data to individual cloud documents
    let mut cloud_documents: Vec<Document> = Vec::new();
    
    if let Some(accounts) = aws_estate_data.as_array() {
        for account in accounts {
            let account_id = account["account_id"].as_str().unwrap_or("unknown");
            
            // Process all services dynamically (not hardcoded)
            if let Some(services) = account["services"].as_object() {
                for (service_name, service_data) in services {
                    // Handle EC2 instances
                    if service_name == "ec2" && service_data["instances"].is_array() {
                        for instance in service_data["instances"].as_array().unwrap() {
                            let doc_id = format!("arn:aws:ec2:{}:{}:instance/{}", 
                                instance["region"].as_str().unwrap_or("us-east-1"),
                                account_id,
                                instance["instance_id"].as_str().unwrap_or("unknown")
                            );
                            let content = format!(
                                "EC2 Instance {} ({}) in region {} - State: {} - Launch Time: {}",
                                instance["name"].as_str().unwrap_or("unnamed"),
                                instance["instance_type"].as_str().unwrap_or("unknown"),
                                instance["region"].as_str().unwrap_or("unknown"),
                                instance["state"].as_str().unwrap_or("unknown"),
                                instance["launch_time"].as_str().unwrap_or("unknown")
                            );
                            
                            let mut metadata = IndexMap::new();
                            metadata.insert("resource_type".to_string(), json!("ec2-instance"));
                            metadata.insert("account_id".to_string(), json!(account_id));
                            metadata.insert("region".to_string(), json!(instance["region"].as_str().unwrap_or("us-east-1")));
                            metadata.insert("service".to_string(), json!(service_name));
                            metadata.insert("state".to_string(), json!(instance["state"].as_str().unwrap_or("unknown")));
                            metadata.insert("tags".to_string(), json!({
                                "name": instance["name"].as_str().unwrap_or("unnamed"),
                                "instance_type": instance["instance_type"].as_str().unwrap_or("unknown")
                            }));
                            metadata.insert("last_synced".to_string(), json!(std::time::SystemTime::now()
                                .duration_since(std::time::UNIX_EPOCH)
                                .unwrap()
                                .as_secs() as i64));
                            
                            // Add IAM permissions dynamically from current service (like Node.js)
                            if let Some(permissions) = service_data["permissions"].as_object() {
                                metadata.insert("iam_permissions".to_string(), json!(permissions));
                            }
                            
                            let doc = Document::new(doc_id, content).with_metadata(metadata);
                            cloud_documents.push(doc);
                        }
                    }
                    
                    // Handle RDS instances  
                    if service_name == "rds" && service_data["instances"].is_array() {
                        for instance in service_data["instances"].as_array().unwrap() {
                            let doc_id = format!("arn:aws:rds:{}:{}:db:{}", 
                                instance["region"].as_str().unwrap_or("us-east-1"),
                                account_id,
                                instance["db_instance_identifier"].as_str().unwrap_or("unknown")
                            );
                            let content = format!(
                                "RDS Database {} ({}) - Engine: {} - Status: {} - Class: {}",
                                instance["name"].as_str().unwrap_or("unnamed"),
                                instance["db_instance_identifier"].as_str().unwrap_or("unknown"),
                                instance["engine"].as_str().unwrap_or("unknown"),
                                instance["db_instance_status"].as_str().unwrap_or("unknown"),
                                instance["db_instance_class"].as_str().unwrap_or("unknown")
                            );
                            
                            let mut metadata = IndexMap::new();
                            metadata.insert("resource_type".to_string(), json!("rds-instance"));
                            metadata.insert("account_id".to_string(), json!(account_id));
                            metadata.insert("region".to_string(), json!(instance["region"].as_str().unwrap_or("us-east-1")));
                            metadata.insert("service".to_string(), json!(service_name));
                            metadata.insert("state".to_string(), json!(instance["db_instance_status"].as_str().unwrap_or("unknown")));
                            metadata.insert("tags".to_string(), json!({
                                "name": instance["name"].as_str().unwrap_or("unnamed"),
                                "engine": instance["engine"].as_str().unwrap_or("unknown")
                            }));
                            metadata.insert("last_synced".to_string(), json!(std::time::SystemTime::now()
                                .duration_since(std::time::UNIX_EPOCH)
                                .unwrap()
                                .as_secs() as i64));
                            
                            // Add IAM permissions dynamically from current service (like Node.js)
                            if let Some(permissions) = service_data["permissions"].as_object() {
                                metadata.insert("iam_permissions".to_string(), json!(permissions));
                            }
                            
                            let doc = Document::new(doc_id, content).with_metadata(metadata);
                            cloud_documents.push(doc);
                        }
                    }
                }
            }
            
        }
    }
    
    println!("✅ Parsed {} cloud resource documents", cloud_documents.len());
    
    // Use enhanced document service to create documents with business architecture
    let create_result = rag_module.create(cloud_documents).await?;
    println!("✅ Created {} documents successfully", create_result.created);
    if create_result.failed.len() > 0 {
        println!("❌ Failed to create {} documents", create_result.failed.len());
        // Print first 3 errors for debugging
        for (i, error) in create_result.failed.iter().take(3).enumerate() {
            println!("   Error {}: {}", i + 1, error);
        }
    }

    // === 2. ENHANCED COLLECTION MANAGER VALIDATION ===
    println!("\\n🏗️ Validating collection architecture...");
    let collections_health = rag_module.collection_manager.get_collections_health().await?;
    println!("✅ Chat Collection ({}): {} points, {}D vectors", 
        collections_health.chat_history.name,
        collections_health.chat_history.points_count,
        collections_health.chat_history.vector_size
    );
    println!("✅ Estate Collection ({}): {} points, {}D vectors",
        collections_health.aws_estate.name, 
        collections_health.aws_estate.points_count,
        collections_health.aws_estate.vector_size
    );
    
    let validation_result = rag_module.collection_manager.validate_collections().await?;
    if validation_result.valid {
        println!("✅ Collection architecture validation passed");
    } else {
        println!("❌ Collection validation issues: {:?}", validation_result.issues);
    }

    // === 3. ENHANCED SEARCH SERVICE TESTING ===
    println!("\\n🔍 Testing enhanced estate search functionality...");
    
    // Test semantic search for AWS estate resources
    let estate_search_options = EstateSearchOptions {
        resource_types: Some(vec!["ec2-instance".to_string()]),
        account_ids: Some(vec!["288761761556".to_string()]),
        regions: Some(vec!["us-east-1".to_string()]),
        services: Some(vec!["ec2".to_string()]),
        states: Some(vec!["running".to_string()]),
        environment: None,
        application: None,
        synced_after: None,
        limit: Some(5),
        score_threshold: Some(0.2),
        include_metadata: true,
        use_anonymous_ids: false,
    };
    
    let estate_results = rag_module.search_service
        .search_estate_resources("running EC2 instances", estate_search_options, None, user_id)
        .await?;
        
    println!("✅ Estate search returned {} results", estate_results.len());
    for (i, result) in estate_results.iter().take(3).enumerate() {
        if let Some(resource_type) = result.get("resource_type") {
            if let Some(account_id) = result.get("account_id") {
                if let Some(state) = result.get("state") {
                    println!("   {}. {} in account {} - state: {}", 
                        i + 1, resource_type, account_id, state);
                }
            }
        }
    }
    
    // === 4. ENHANCED ENCRYPTION SERVICE TESTING ===
    println!("\\n🔐 Testing enhanced encryption service...");
    
    // Test encryption status
    let encryption_status = rag_module.encryption_service.get_status();
    println!("✅ Encryption Service Status:");
    println!("   - Initialized: {:?}", encryption_status.get("initialized"));
    println!("   - Algorithm: {:?}", encryption_status.get("algorithm"));
    println!("   - Content Encryption: {:?}", encryption_status.get("content_encryption"));
    println!("   - Metadata Encryption: {:?}", encryption_status.get("metadata_encryption"));
    
    // Test secure ID generation
    let secure_id = rag_module.encryption_service.generate_secure_id(Some(16))?;
    println!("✅ Generated secure ID: {}", secure_id);
    
    // === 5. ENHANCED DOCUMENT SERVICE FEATURES ===
    println!("\\n📄 Testing enhanced document service features...");
    
    // Test document count by filters
    let filter = json!({"resource_type": "ec2-instance"});
    let ec2_count = rag_module.get_document_count(Some(filter)).await?;
    println!("✅ Total EC2 instances in storage: {}", ec2_count);
    
    // Test document listing with pagination
    let list_options = json!({
        "filter": {
            "service": "rds"
        },
        "limit": 3,
        "offset": 0
    });
    
    let document_list = rag_module.list_documents(Some(list_options)).await?;
    println!("✅ Listed {} RDS documents", document_list.len());
    
    // === 6. COMPREHENSIVE SUMMARY ===
    // === FINAL SUMMARY ===
    println!("\\n");
    println!("═══════════════════════════════════════════════════════════");
    println!("                    MODE SUMMARY                           ");
    println!("═══════════════════════════════════════════════════════════");
    println!("Current Mode: {}", mode);
    println!();

    if mode == "💾 Embedded Mode" {
        println!("✅ Embedded Mode Benefits:");
        println!("   • No server required");
        println!("   • Simple setup");
        println!("   • Fast for small datasets");
        println!("   • Perfect for development");
        println!();
        println!("📦 Data stored in: ./example-enhanced-estate-data/qdrant-data/");
        println!();
        println!("🚀 Want to try Server Mode?");
        println!("   docker-compose up -d");
        println!("   export QDRANT_URL=\\\"http://localhost:6334\\\"");
        println!("   cargo run --example aws_estate_example");
    } else {
        println!("✅ Server Mode Benefits:");
        println!("   • Production-ready");
        println!("   • Scalable to millions of docs");
        println!("   • Web dashboard available");
        println!("   • Multi-app access");
        println!();
        println!("📦 Data in Qdrant server + local backup");
        println!("🌐 Dashboard: http://localhost:6333/dashboard");
        println!();
        println!("💾 Want to try Embedded Mode?");
        println!("   unset QDRANT_URL");
        println!("   cargo run --example aws_estate_example");
    }

    println!();
    println!("📚 Key Insight: Same code, same folder structure!");
    println!("   The ONLY difference is where vectors are stored.");
    println!();
    println!("📋 Features Demonstrated:");
    println!("✅ AWS Estate Data Processing");
    println!("   - Estate point ID generation");
    println!("   - Cloud resource classification");
    println!("   - Multi-service document creation");
    println!("✅ Semantic Search with BGE-M3");
    println!("   - 1024D vector embeddings");
    println!("   - Collection-specific search");
    println!("   - Business architecture filtering");
    println!("✅ Encryption & Security");
    println!("   - OS keychain integration");
    println!("   - Content and metadata encryption");
    println!("   - Secure ID generation");
    println!("═══════════════════════════════════════════════════════════");

    Ok(())
}