use std::env;
use std::process::ExitCode;
use std::time::Duration;

use sgclaw::{parse_probe_args, run_probe_script, ProbeOutcome};

fn main() -> ExitCode {
    match run() {
        Ok(()) => ExitCode::SUCCESS,
        Err(err) => {
            eprintln!("sgbrowser_ws_probe failed: {err}");
            ExitCode::FAILURE
        }
    }
}

fn run() -> Result<(), String> {
    let args: Vec<String> = env::args().skip(1).collect();
    let config = match parse_probe_args(&args) {
        Ok(config) => config,
        Err(err) => return Err(err.to_string()),
    };
    let results = match run_probe_script(
        &config.ws_url,
        Duration::from_millis(config.timeout_ms),
        config.steps,
    ) {
        Ok(results) => results,
        Err(err) => return Err(err.to_string()),
    };

    for (index, result) in results.iter().enumerate() {
        println!("STEP {} {}", index + 1, result.label);
        println!("SEND: {}", result.sent);
        match &result.outcome {
            ProbeOutcome::Received(frames) => {
                if frames.is_empty() {
                    println!("RECV: <none>");
                } else {
                    for frame in frames {
                        println!("RECV: {}", frame);
                    }
                }
                println!("OUTCOME: received");
            }
            ProbeOutcome::NoReplyExpected => {
                println!("RECV: <none>");
                println!("OUTCOME: no-reply-expected");
            }
            ProbeOutcome::TimedOut => {
                println!("RECV: <none>");
                println!("OUTCOME: timeout");
            }
            ProbeOutcome::Closed => {
                println!("RECV: <none>");
                println!("OUTCOME: closed");
            }
            ProbeOutcome::ConnectFailed(message) => {
                println!("RECV: <none>");
                println!("OUTCOME: connect-failed");
                println!("DETAIL: {}", message);
            }
        }
        if index + 1 < results.len() {
            println!();
        }
    }

    Ok(())
}
