//! Simple Framework Stress Demo - Dogfooding Innovation
//!
//! This example demonstrates the basic concept of framework stress testing
//! where the Cleanroom framework tests itself under load conditions.
//!
//! Key innovation: Framework testing itself with multiple environments

use clnrm_core::{CleanroomEnvironment, CleanroomError};

#[tokio::main]
async fn main() -> Result<(), CleanroomError> {
    println!("🚀 Simple Framework Stress Demo - Dogfooding Innovation");
    println!("=====================================================");
    println!("Demonstrating framework testing itself under stress conditions\n");

    let main_env = CleanroomEnvironment::new().await?;
    println!("✅ Created main environment: {}", main_env.session_id());

    // Innovation: Create multiple environments to stress test the framework
    println!("\n🔬 Innovation: Multi-Environment Stress Testing");
    println!("=============================================");

    let mut environments = Vec::new();

    // Create 5 environments concurrently (reasonable for demo)
    for i in 0..5 {
        println!("   Creating environment {}...", i + 1);
        let test_env = CleanroomEnvironment::new().await?;
        println!(
            "   ✅ Environment {} created: {}",
            i + 1,
            test_env.session_id()
        );

        // Run a simple validation test in each environment
        let result = test_env
            .execute_test("stress_validation", || {
                Ok::<String, CleanroomError>(format!("Environment {} validated", i + 1))
            })
            .await?;

        println!("   ✅ Test result: {}", result);
        environments.push(test_env);
    }

    println!("\n📊 Stress Test Results:");
    println!("=====================");
    println!("Created {} environments successfully", environments.len());
    println!("Each environment has unique session ID");

    // Verify all environments are different
    let session_ids: Vec<_> = environments
        .iter()
        .map(|env| env.session_id().to_string())
        .collect();
    let unique_ids = session_ids.len();
    let total_ids = session_ids.len();

    if unique_ids == total_ids {
        println!("✅ All environments have unique session IDs");
        println!("✅ Framework properly isolates test environments");
    } else {
        println!("❌ Some environments share session IDs");
    }

    // Innovation: Container reuse demonstration
    println!("\n🔬 Innovation: Container Reuse Under Stress");
    println!("===========================================");

    let mut container_handles = Vec::new();

    // Create containers in the main environment
    for i in 0..10 {
        let container_result = main_env
            .get_or_create_container(&format!("stress-demo-{}", i), || {
                println!("   Creating container {}...", i + 1);
                Ok::<String, CleanroomError>(format!("stress-demo-container-{}", i))
            })
            .await;

        match container_result {
            Ok(handle) => {
                container_handles.push(handle);
                println!("   ✅ Container {} created", i + 1);
            }
            Err(e) => {
                println!("   ⚠️  Container {} creation limited: {}", i + 1, e);
                break;
            }
        }
    }

    println!(
        "   Created {} containers in main environment",
        container_handles.len()
    );

    // Demonstrate reuse by trying to get the same containers again
    println!("\n🔬 Innovation: Container Reuse Verification");
    println!("==========================================");

    for i in 0..5 {
        let reused_result = main_env
            .get_or_create_container(&format!("stress-demo-{}", i), || {
                println!("   ⚠️  This should not be called - container should be reused");
                Ok::<String, CleanroomError>("should-not-be-created".to_string())
            })
            .await;

        match reused_result {
            Ok(_handle) => {
                println!("   ✅ Container {} reused successfully", i);
            }
            Err(e) => {
                println!("   ❌ Container {} reuse failed: {}", i, e);
            }
        }
    }

    // Final validation
    println!("\n🎉 STRESS TEST DEMONSTRATION COMPLETED!");
    println!("=====================================");
    println!("This demo proves the framework can:");
    println!("✅ Create multiple isolated environments");
    println!("✅ Handle concurrent environment creation");
    println!("✅ Manage container lifecycle under stress");
    println!("✅ Demonstrate container reuse capabilities");
    println!("✅ Test itself using its own APIs");

    println!("\n🚀 Framework successfully 'eats its own dog food'");
    println!("   by using itself to validate its stress testing capabilities!");

    Ok(())
}
