use std::fs;
use std::path::PathBuf;

use serde::Serialize;

pub(crate) fn write_temp_json<T: Serialize>(stem: &str, value: &T) -> PathBuf {
    let nonce = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .expect("system time should be after epoch")
        .as_nanos();
    let path = std::env::temp_dir().join(format!("rrt-cli-{stem}-{nonce}.json"));
    let bytes = serde_json::to_vec_pretty(value).expect("json serialization should succeed");
    fs::write(&path, bytes).expect("temp json should be written");
    path
}
