// src/recall-audit.ts
import { appendFile, mkdir, readdir, rm } from "fs/promises";
import path from "path";
function formatIsoDate(ts) {
  const normalized = new Date(ts);
  if (Number.isNaN(normalized.getTime())) {
    return (/* @__PURE__ */ new Date()).toISOString().slice(0, 10);
  }
  return normalized.toISOString().slice(0, 10);
}
function buildRecallAuditPath(rootDir, ts, sessionKey) {
  const safeSessionKey = encodeURIComponent(sessionKey);
  return path.join(rootDir, "transcripts", formatIsoDate(ts), `${safeSessionKey}.jsonl`);
}
async function appendRecallAuditEntry(rootDir, entry) {
  const filePath = buildRecallAuditPath(rootDir, entry.ts, entry.sessionKey);
  await mkdir(path.dirname(filePath), { recursive: true });
  await appendFile(filePath, `${JSON.stringify(entry)}
`, "utf8");
  return filePath;
}
async function pruneRecallAuditEntries(rootDir, retentionDays, now = /* @__PURE__ */ new Date()) {
  const transcriptsDir = path.join(rootDir, "transcripts");
  const removed = [];
  const cutoff = new Date(now);
  cutoff.setUTCHours(0, 0, 0, 0);
  cutoff.setUTCDate(cutoff.getUTCDate() - Math.max(1, Math.floor(retentionDays)));
  let entries;
  try {
    entries = await readdir(transcriptsDir, { withFileTypes: true });
  } catch {
    return removed;
  }
  for (const entry of entries) {
    if (!entry.isDirectory()) continue;
    if (!/^\d{4}-\d{2}-\d{2}$/.test(entry.name)) continue;
    const day = /* @__PURE__ */ new Date(`${entry.name}T00:00:00.000Z`);
    if (Number.isNaN(day.getTime()) || day >= cutoff) continue;
    const dirPath = path.join(transcriptsDir, entry.name);
    await rm(dirPath, { recursive: true, force: true });
    removed.push(dirPath);
  }
  return removed;
}

export {
  buildRecallAuditPath,
  appendRecallAuditEntry,
  pruneRecallAuditEntries
};
//# sourceMappingURL=chunk-LBLXEFWK.js.map