#![allow(clippy::module_inception)] // Module name matches parent for re-export patterns
//! Load Testing Binary - Throughput Validator
//!
//! Orchestrates and runs comprehensive load tests for trading service

use anyhow::Result;
use clap::Parser;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

mod clients;
mod metrics;
mod scenarios;

#[derive(Parser, Debug)]
#[command(name = "throughput_validator")]
#[command(about = "Trading Service Load Testing and Throughput Validation", long_about = None)]
struct Args {
    /// Test scenario to run
    #[arg(short, long, default_value = "all")]
    scenario: String,

    /// Trading service URL
    #[arg(short, long, default_value = "http://localhost:50051")]
    url: String,

    /// Output report path
    #[arg(short, long, default_value = "/tmp/WAVE_120_AGENT_5_LOAD_TESTING.md")]
    output: String,

    /// Enable verbose logging
    #[arg(short, long)]
    verbose: bool,
}

#[tokio::main]
async fn main() -> Result<()> {
    let args = Args::parse();

    // Initialize tracing
    let log_level = if args.verbose { "debug" } else { "info" };
    tracing_subscriber::registry()
        .with(
            tracing_subscriber::EnvFilter::try_from_default_env()
                .unwrap_or_else(|_| format!("load_tests={log_level},tower_http=debug").into()),
        )
        .with(tracing_subscriber::fmt::layer())
        .init();

    println!("\n🚀 Foxhunt Load Testing - Throughput Validator");
    let separator = "=".repeat(80);
    println!("{separator}\n");

    // Run selected scenario
    let report = match args.scenario.as_str() {
        "sustained" => scenarios::sustained_load::run(&args.url).await?,
        "burst" => scenarios::burst_load::run(&args.url).await?,
        "streaming" => scenarios::streaming_load::run(&args.url).await?,
        "pool" => scenarios::pool_saturation::run(&args.url).await?,
        "all" => scenarios::comprehensive::run(&args.url).await?,
        _ => {
            let scenario = &args.scenario;
            tracing::error!("❌ Unknown scenario: {scenario}");
            tracing::info!("Available scenarios: sustained, burst, streaming, pool, all");
            std::process::exit(1);
        },
    };

    // Write report to file
    tokio::fs::write(&args.output, report.to_markdown()).await?;

    println!("\n✅ Load testing complete!");
    let output = &args.output;
    println!("📊 Report saved to: {output}");
    let separator = "=".repeat(80);
    println!("{separator}\n");

    Ok(())
}
