use std::path::PathBuf;
use std::process::Command;
use std::time::{SystemTime, UNIX_EPOCH};

#[test]
fn module_c_cli_round_trip_v1_to_v2() {
    let binary = env!("CARGO_BIN_EXE_tracer-bullet");
    let wal_path = unique_wal_path("module-c-cli-roundtrip");

    let write = Command::new(binary)
        .args([
            "module-c-write",
            "--records",
            "5000",
            "--wal-path",
            wal_path.to_str().expect("WAL path should be UTF-8"),
            "--json",
        ])
        .output()
        .expect("failed to run module-c-write");

    assert!(
        write.status.success(),
        "module-c-write failed: status={:?} stderr={} stdout={}",
        write.status,
        String::from_utf8_lossy(&write.stderr),
        String::from_utf8_lossy(&write.stdout)
    );

    let read = Command::new(binary)
        .args([
            "module-c-read",
            "--wal-path",
            wal_path.to_str().expect("WAL path should be UTF-8"),
            "--expect-records",
            "5000",
            "--json",
        ])
        .output()
        .expect("failed to run module-c-read");

    assert!(
        read.status.success(),
        "module-c-read failed: status={:?} stderr={} stdout={}",
        read.status,
        String::from_utf8_lossy(&read.stderr),
        String::from_utf8_lossy(&read.stdout)
    );

    let stdout = String::from_utf8_lossy(&read.stdout);
    assert!(stdout.contains("\"defaults_ok\":true"));
    assert!(stdout.contains("\"zero_copy_ok\":true"));

    std::fs::remove_file(wal_path).expect("temporary WAL should be removed");
}

#[test]
#[ignore = "heavy million-record CLI scan"]
fn module_c_cli_million_records() {
    let binary = env!("CARGO_BIN_EXE_tracer-bullet");
    let wal_path = unique_wal_path("module-c-cli-million");

    let write_status = Command::new(binary)
        .args([
            "module-c-write",
            "--records",
            "1000000",
            "--wal-path",
            wal_path.to_str().expect("WAL path should be UTF-8"),
            "--json",
        ])
        .status()
        .expect("failed to run module-c-write million test");
    assert!(write_status.success(), "module-c-write million test failed");

    let read_status = Command::new(binary)
        .args([
            "module-c-read",
            "--wal-path",
            wal_path.to_str().expect("WAL path should be UTF-8"),
            "--expect-records",
            "1000000",
            "--json",
        ])
        .status()
        .expect("failed to run module-c-read million test");
    assert!(read_status.success(), "module-c-read million test failed");

    std::fs::remove_file(wal_path).expect("temporary WAL should be removed");
}

fn unique_wal_path(prefix: &str) -> PathBuf {
    let nanos = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("system time should be valid")
        .as_nanos();
    std::env::temp_dir().join(format!("{}-{}-{}.bin", prefix, std::process::id(), nanos))
}
