//! Simplified RAG Storage Example
//!
//! This example shows how to store AWS estate data as simple points in Qdrant
//! Each JSON object is stored as ONE point with its embedding

#![recursion_limit = "256"]

use anyhow::Result;
use rag_module::*;
use serde_json::json;

#[tokio::main]

async fn main() -> Result<()> {
    println!("🧪 Simplified RAG Storage Example");
    println!("==================================\n");

    // Initialize RAG module
    let rag = RagModule::new("./test_data1").await?;
    rag.initialize().await?;

    let user_id = "test_user_123";
    let collection_name = "detailed_estate"; // Can be changed to "aws_estate", "azure_estate", "gcp_estate", etc.
    let dimensions = 1024; // BGE-M3 embedding dimensions

    // Create collection if it doesn't exist (similar to PlaybookStoreService)
    println!("🔄 Attempting to create collection '{}' with {} dimensions...", collection_name, dimensions);
    match rag.vector_store.create_collection(collection_name, dimensions).await {
        Ok(_) => {
            println!("✅ Collection '{}' initialized ({} dimensions)", collection_name, dimensions);
            // Wait a moment for collection to be fully ready
            tokio::time::sleep(std::time::Duration::from_millis(100)).await;
        },
        Err(e) => {
            // Check if collection already exists (common case)
            let error_msg = e.to_string();
            if error_msg.contains("already exists") || error_msg.contains("Collection exists") || error_msg.contains("exists") {
                println!("ℹ️  Collection '{}' already exists, skipping creation", collection_name);
            } else {
                eprintln!("⚠️  Failed to create collection '{}': {} - continuing anyway", collection_name, e);
            }
        }
    }

    // Example 1: Store a single EC2 instance
    println!("\n📥 Example 1: Storing a single EC2 instance to collection: {}", collection_name);
    let ec2_instance_1 = json!({
        "type": "ec2",
        "keywords": [
          "ec2",
          "instance",
          "compute"
        ],
        "profile": "dev",
        "accountId": "288761761556",
        "region": "ap-south-1",
        "permissions": {
          "accessLevel": "ReadOnly",
          "hasRead": true,
          "hasWrite": false,
          "hasFullAccess": false
        },
        "content": "EC2 instance ui-agent-parse, id i-0a900c6db2f2a3c8e, region ap-south-1, type c4.large, platform Linux/UNIX, state running, volumeCount 1, launched 2025-10-30T10:57:11+00:00",
        "architecture": "x86_64",
        "blockDeviceMappings": [
          {
            "deviceName": "/dev/xvda",
            "ebs": {
              "attachTime": "2025-10-30T10:57:11+00:00",
              "deleteOnTermination": true,
              "status": "attached",
              "volumeId": "vol-012b06d9959024a25"
            }
          }
        ],
        "clientToken": "e0eb0a1e-a8d0-423e-aa6e-3e1ee5cd805a",
        "ebsOptimized": true,
        "enaSupport": true,
        "hypervisor": "xen",
        "networkInterfaces": [
          {
            "association": {
              "ipOwnerId": "amazon",
              "publicDnsName": "ec2-3-108-219-193.ap-south-1.compute.amazonaws.com",
              "publicIp": "3.108.219.193"
            },
            "attachment": {
              "attachTime": "2025-10-30T10:57:11+00:00",
              "attachmentId": "eni-attach-01c81dc0c7981248c",
              "deleteOnTermination": true,
              "deviceIndex": 0,
              "status": "attached",
              "networkCardIndex": 0
            },
            "description": "",
            "groups": [
              {
                "groupId": "sg-0127576b45dcfc18a",
                "groupName": "web-server-sg"
              }
            ],
            "ipv6Addresses": [],
            "macAddress": "0a:9e:d8:43:5e:3d",
            "networkInterfaceId": "eni-0454d06e6a2858078",
            "ownerId": "288761761556",
            "privateDnsName": "ip-172-31-10-145.ap-south-1.compute.internal",
            "privateIpAddress": "172.31.10.145",
            "privateIpAddresses": [
              {
                "association": {
                  "ipOwnerId": "amazon",
                  "publicDnsName": "ec2-3-108-219-193.ap-south-1.compute.amazonaws.com",
                  "publicIp": "3.108.219.193"
                },
                "primary": true,
                "privateDnsName": "ip-172-31-10-145.ap-south-1.compute.internal",
                "privateIpAddress": "172.31.10.145"
              }
            ],
            "sourceDestCheck": true,
            "status": "in-use",
            "subnetId": "subnet-0e53cf9e1b79b9ea8",
            "vpcId": "vpc-0ea458cebbbfa92b0",
            "interfaceType": "interface",
            "operator": {
              "managed": false
            }
          }
        ],
        "rootDeviceName": "/dev/xvda",
        "rootDeviceType": "ebs",
        "securityGroups": [
          {
            "groupId": "sg-0127576b45dcfc18a",
            "groupName": "web-server-sg"
          }
        ],
        "sourceDestCheck": true,
        "tags": [
          {
            "key": "Name",
            "value": "ui-agent-parse"
          }
        ],
        "virtualizationType": "hvm",
        "cpuOptions": {
          "coreCount": 1,
          "threadsPerCore": 2
        },
        "capacityReservationSpecification": {
          "capacityReservationPreference": "open"
        },
        "hibernationOptions": {
          "configured": false
        },
        "metadataOptions": {
          "state": "applied",
          "httpTokens": "required",
          "httpPutResponseHopLimit": 2,
          "httpEndpoint": "enabled",
          "httpProtocolIpv6": "disabled",
          "instanceMetadataTags": "disabled"
        },
        "enclaveOptions": {
          "enabled": false
        },
        "bootMode": "uefi-preferred",
        "platformDetails": "Linux/UNIX",
        "usageOperation": "RunInstances",
        "usageOperationUpdateTime": "2025-10-30T10:57:11+00:00",
        "privateDnsNameOptions": {
          "hostnameType": "ip-name",
          "enableResourceNameDnsARecord": true,
          "enableResourceNameDnsAAAARecord": false
        },
        "maintenanceOptions": {
          "autoRecovery": "default",
          "rebootMigration": "default"
        },
        "currentInstanceBootMode": "legacy-bios",
        "networkPerformanceOptions": {
          "bandwidthWeighting": "default"
        },
        "operator": {
          "managed": false
        },
        "instanceId": "i-0a900c6db2f2a3c8e",
        "imageId": "ami-01760eea5c574eb86",
        "state": {
          "code": 16,
          "name": "running"
        },
        "privateDnsName": "ip-172-31-10-145.ap-south-1.compute.internal",
        "publicDnsName": "ec2-3-108-219-193.ap-south-1.compute.amazonaws.com",
        "stateTransitionReason": "",
        "keyName": "sd_key",
        "amiLaunchIndex": 0,
        "productCodes": [],
        "instanceType": "c4.large",
        "launchTime": "2025-10-30T10:57:11+00:00",
        "placement": {
          "availabilityZoneId": "aps1-az3",
          "groupName": "",
          "tenancy": "default",
          "availabilityZone": "ap-south-1b"
        },
        "monitoring": {
          "state": "disabled"
        },
        "subnetId": "subnet-0e53cf9e1b79b9ea8",
        "vpcId": "vpc-0ea458cebbbfa92b0",
        "privateIpAddress": "172.31.10.145",
        "publicIpAddress": "3.108.219.193",
        "volumeDetails": [
          {
            "volumeId": "vol-012b06d9959024a25",
            "device": "/dev/xvda",
            "size": 8,
            "type": "gp3",
            "deleteOnTermination": true
          }
        ]
    });

    let _result1 = rag.ingest_aws_estate(ec2_instance_1, user_id, collection_name).await?;
    println!("✅ EC2 instance stored as 1 point in '{}' collection", collection_name);

 

    println!("\n🎉 Simplified ingestion completed successfully!");
    println!("✅ Total documents stored: 1");
    println!("✅ Collection used: '{}'", collection_name);
    println!("✅ Each JSON object stored as one point in Qdrant");
    println!("✅ Each point has its own embedding");
    println!("✅ No metadata extraction, no complex parsing");
    println!("\n💡 Tip: You can change 'collection_name' to use different collections:");
    println!("   - aws_estate (AWS resources)");
    println!("   - azure_estate (Azure resources)");
    println!("   - gcp_estate (GCP resources)");
    println!("   - core_estate (default mixed cloud resources)");

    Ok(())
}
 
