use std::path::PathBuf;

use serde::Serialize;

pub(crate) fn unique_temp_path(stem: &str, extension: &str) -> PathBuf {
    let nonce = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .expect("system time should be after epoch")
        .as_nanos();
    std::env::temp_dir().join(format!("rrt-{stem}-{nonce}.{extension}"))
}

pub(crate) fn write_temp_json<T: Serialize>(stem: &str, value: &T) -> PathBuf {
    let path = unique_temp_path(stem, "json");
    let bytes = serde_json::to_vec_pretty(value).expect("json serialization should succeed");
    std::fs::write(&path, bytes).expect("temp json should be written");
    path
}
