use std::env;
use std::time::Instant;
use trazaeo::hashing::{hash_file_streaming, DEFAULT_STREAM_CHUNK_SIZE};

/// Parses or default.
fn parse_or_default<T: std::str::FromStr>(value: Option<&String>, default: T) -> T {
    value.and_then(|v| v.parse::<T>().ok()).unwrap_or(default)
}

/// Runs the entry point for this executable.
fn main() {
    let args: Vec<String> = env::args().collect();
    if args.len() < 2 {
        eprintln!("usage: cargo run --example perf_hashing -- <path> [chunk_size_bytes] [threads]");
        std::process::exit(2);
    }

    let path = &args[1];
    let chunk_size = parse_or_default(args.get(2), DEFAULT_STREAM_CHUNK_SIZE);
    let threads = parse_or_default(args.get(3), 4usize);

    let size_bytes = std::fs::metadata(path).map(|m| m.len()).unwrap_or_default();
    let started = Instant::now();
    let (hashes, root) = hash_file_streaming(path, chunk_size, threads).expect("hash file");
    let elapsed = started.elapsed();
    let seconds = elapsed.as_secs_f64().max(1e-9);
    let mib_per_s = (size_bytes as f64 / (1024.0 * 1024.0)) / seconds;

    println!("path={path}");
    println!("bytes={size_bytes}");
    println!("chunk_size={chunk_size}");
    println!("threads={threads}");
    println!("chunks={}", hashes.len());
    println!("elapsed_seconds={seconds:.6}");
    println!("throughput_mib_per_s={mib_per_s:.3}");
    println!("root_hash={}", hex::encode(root.0));
}
