use anyhow::Result;
use clap::Args;
use crate::runner;
use super::node_utils::print_nodes_table;

#[derive(Args)]
pub struct PrintArgs {
    #[arg(required = true)]
    pub output_dirs: Vec<String>,
    #[arg(short, long, num_args = 1..)]
    pub metrics: Option<Vec<String>>,
}

pub fn run(args: PrintArgs) -> Result<()> {
    let metrics: Vec<&str> = args.metrics.as_ref()
        .map(|m| m.iter().map(|s| s.as_str()).collect())
        .unwrap_or_else(|| vec!["success", "throughput", "p90"]);

    for dir in &args.output_dirs {
        let test = runner::load_test_payload(dir)?;
        let results = runner::load_results_payload(dir)?;

        println!("\n╔══════════════════════════════════════╗");
        println!("║  load test: {:<24} ║", test.name);
        println!("╚══════════════════════════════════════╝\n");

        print_nodes_table(&results.nodes);
        runner::print_metric_tables(&results.results, &metrics);
    }
    Ok(())
}
