use std::path::PathBuf;
use crate::error::Result;
use crate::session::{mtime_epoch, plans_dir};
pub fn latest_plan_file(since_epoch: u64) -> Result<Option<(PathBuf, Option<String>)>> {
let dir = plans_dir()?;
if !dir.exists() {
return Ok(None);
}
let mut best: Option<(u64, PathBuf)> = None;
for entry in std::fs::read_dir(&dir).map_err(|e| crate::error::Error::io(&dir, e))? {
let Ok(entry) = entry else { continue };
let path = entry.path();
let is_plan = path
.extension()
.and_then(|e| e.to_str())
.map(|e| e.eq_ignore_ascii_case("md"))
.unwrap_or(false);
if !is_plan {
continue;
}
let Some(mtime) = mtime_epoch(&path) else { continue };
if mtime < since_epoch {
continue;
}
if best.as_ref().map(|(t, _)| mtime > *t).unwrap_or(true) {
best = Some((mtime, path));
}
}
Ok(best.map(|(_, path)| {
let content = std::fs::read_to_string(&path).ok();
(path, content)
}))
}