use std::path::{Path, PathBuf};
use std::time::SystemTime;
use anyhow::Result;
use serde_json::json;
fn utc(t: SystemTime) -> String {
let secs = t
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0) as i64;
let (days, rest) = (secs.div_euclid(86_400), secs.rem_euclid(86_400));
let z = days + 719_468;
let era = z.div_euclid(146_097);
let doe = z - era * 146_097;
let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
let mp = (5 * doy + 2) / 153;
let d = doy - (153 * mp + 2) / 5 + 1;
let m = if mp < 10 { mp + 3 } else { mp - 9 };
let y = yoe + era * 400 + i64::from(m <= 2);
format!(
"{y:04}-{m:02}-{d:02}T{:02}:{:02}:{:02}Z",
rest / 3600,
rest % 3600 / 60,
rest % 60
)
}
pub fn path_of(output: &Path) -> PathBuf {
let mut name = output.file_name().unwrap_or_default().to_os_string();
name.push(".receipt.json");
output.with_file_name(name)
}
pub const CHAIN: &str = ".gridpin-chain";
pub fn write(
sheet: &Path,
input: &Path,
output: &Path,
rows: u64,
started: SystemTime,
) -> Result<PathBuf> {
let hash = crate::sheets::sha256_file;
let file = |p: &Path| {
p.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_default()
};
let chain = output.with_file_name(CHAIN);
let prev = std::fs::read_to_string(&chain)
.ok()
.map(|s| s.trim().to_string())
.filter(|s| s.len() == 64);
let body = json!({
"receipt": "gridpin batch",
"engine": env!("CARGO_PKG_VERSION"),
"command": std::env::args().collect::<Vec<_>>(),
"sheet": {"file": file(sheet), "sha256": hash(sheet)?},
"input": {"file": file(input), "sha256": hash(input)?},
"output": {"file": file(output), "sha256": hash(output)?},
"rows": rows,
"started": utc(started),
"finished": utc(SystemTime::now()),
"prev": prev,
});
let path = path_of(output);
std::fs::write(&path, serde_json::to_string_pretty(&body)? + "\n")?;
std::fs::write(&chain, hash(&path)? + "\n")?;
Ok(path)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_time_is_utc_iso_and_the_receipt_sits_beside_the_output() {
let t = std::time::UNIX_EPOCH + std::time::Duration::from_secs(1_794_141_075);
assert_eq!(utc(t), "2026-11-08T12:31:15Z");
assert_eq!(utc(std::time::UNIX_EPOCH), "1970-01-01T00:00:00Z");
assert_eq!(
utc(std::time::UNIX_EPOCH + std::time::Duration::from_secs(951_782_400)),
"2000-02-29T00:00:00Z"
);
assert_eq!(
path_of(Path::new("out/a.csv")),
Path::new("out/a.csv.receipt.json")
);
}
}