const statusPath = "150 Audio Recordings/Processing Status/audio-processing-status.json";
 
const days = Number(dv.current()["Days"] ?? 14);
 
const now = new Date();
const cutoff = new Date(now);
cutoff.setDate(cutoff.getDate() - days);
 
let rows = [];
 
try {
  const raw = await app.vault.adapter.read(statusPath);
  rows = JSON.parse(raw);
} catch (e) {
  dv.paragraph("No processing status file found, or the JSON could not be read.");
}
 
function parseDate(value) {
  if (!value) return null;
  const normalised = String(value).replace(" ", "T");
  const date = new Date(normalised);
  return isNaN(date) ? null : date;
}
 
function badge(status) {
  if (status === "completed") return "✅ Completed";
  if (status === "failed") return "❌ Failed";
  if (status === "processing") return "🔄 Processing";
  if (status === "queued") return "⏳ Queued";
  return status ?? "Unknown";
}
 
const processed = rows
  .map(r => ({
    ...r,
    updatedDate: parseDate(r.updated),
    firstSeenDate: parseDate(r.first_seen)
  }))
  .filter(r => r.updatedDate && r.updatedDate >= cutoff)
  .sort((a, b) => b.updatedDate - a.updatedDate);
 
dv.paragraph(`Showing processing activity from the last ${days} days.`);
 
dv.table(
  ["File", "First Seen", "Status", "Stage", "Updated", "Message", "Error"],
  processed.map(r => [
    r.file,
    r.first_seen ?? "",
    badge(r.status),
    r.stage ?? "",
    r.updated ?? "",
    r.message ?? "",
    r.error ?? ""
  ])
);
const days = Number(dv.current()["Days"] ?? 14);
 
const transcriptFolder = "150 Audio Recordings"; // adjust if needed
 
const now = new Date();
const cutoff = new Date(now);
cutoff.setDate(cutoff.getDate() - days);
 
function fileDateInfo(p) {
  const d = p.date ?? p.file.ctime;
 
  if (typeof d?.toFormat === "function") {
    const key = d.toFormat("yyyy-MM-dd");
    return { date: new Date(`${key}T00:00:00`), key };
  }
 
  if (typeof d?.toISODate === "function") {
    const key = d.toISODate();
    return { date: new Date(`${key}T00:00:00`), key };
  }
 
  const match = String(d).match(/^(\d{4})-(\d{2})-(\d{2})/);
  if (match) {
    const key = `${match[1]}-${match[2]}-${match[3]}`;
    return { date: new Date(`${key}T00:00:00`), key };
  }
 
  const date = new Date(d);
  if (Number.isNaN(date.getTime())) return null;
  const key = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}-${String(date.getDate()).padStart(2, "0")}`;
  return { date, key };
}
 
let pages = dv.pages(`"${transcriptFolder}"`)
  .where(p => p.file.name !== dv.current().file.name)
  .where(p => p.file.ext === "md")
  .map(p => {
    const dateInfo = fileDateInfo(p);
    return {
      page: p,
      date: dateInfo?.date ?? null,
      dateKey: dateInfo?.key ?? "",
      name: p.file.name
    };
  })
  .where(x => x.date && !isNaN(x.date) && x.date >= cutoff)
  .array();
 
// Sort by filename (descending = newest first if names are ISO-like)
pages.sort((a, b) => b.name.localeCompare(a.name, undefined, { numeric: true }));
 
dv.paragraph(`Showing transcripts from the last ${days} days.`);
 
dv.table(
  ["Transcript", "Date", "Folder"],
  pages.map(x => [
    x.page.file.link,
    x.dateKey,
    x.page.file.folder
  ])
);