use crate::cli_util::parse_duration_ms;
use anyhow::Result;
use mcpspy::core::db::TelemetryDb;
use std::path::Path;
pub fn cutoff_ms(now_ms: i64, older_than: &str) -> Result<i64> {
Ok(now_ms - parse_duration_ms(older_than)?)
}
pub fn run(db_path: &str, older_than: &str) -> Result<()> {
let db = TelemetryDb::open(Path::new(db_path))?;
let now_ms = chrono::Utc::now().timestamp_millis();
let cutoff = cutoff_ms(now_ms, older_than)?;
let rt = tokio::runtime::Runtime::new()?;
let deleted = rt.block_on(db.prune(cutoff))?;
println!("Pruned {deleted} record(s)/violation(s) older than {older_than}.");
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cutoff_subtracts_duration_from_now() {
assert_eq!(cutoff_ms(1_000_000, "7d").unwrap(), 1_000_000 - 604_800_000);
assert_eq!(cutoff_ms(500_000, "1h").unwrap(), 500_000 - 3_600_000);
}
#[test]
fn cutoff_rejects_invalid_duration() {
assert!(cutoff_ms(1_000_000, "abc").is_err());
}
}