#!/usr/bin/env node
/**
 * @agent-manifest
 * name:        list-drafts
 * title:       Draft Lister
 * class:       plumbing
 * trigger:     manual
 * description: List pending drafts awaiting review. No LLM.
 * model:       -
 * prompt:      -
 * skills:      -
 * tools:       -
 * reads:       -
 * writes:      -
 * created:     2026-07-04
 * @end
 */
// List pending drafts awaiting review.
// Usage: node agents/scripts/list-drafts.mjs
import { readdirSync, existsSync, readFileSync } from "node:fs";
import { dirname, resolve, join } from "node:path";
import { fileURLToPath } from "node:url";
import { loadJson, readFrontmatter, fmString } from "./lib/util.mjs";

const HERE = dirname(fileURLToPath(import.meta.url));
const PIPELINE = resolve(HERE, "..");
const cfg = loadJson(join(PIPELINE, "config.json"));
const draftsRoot = join(cfg.paths.projectRoot, cfg.paths.draftsDir);

if (!existsSync(draftsRoot)) {
  console.log("No drafts directory yet.");
  process.exit(0);
}
const dirs = readdirSync(draftsRoot, { withFileTypes: true }).filter((d) => d.isDirectory());
if (!dirs.length) {
  console.log("No drafts pending.");
  process.exit(0);
}

console.log(`Pending drafts (${dirs.length}):\n`);
for (const d of dirs) {
  const mdx = join(draftsRoot, d.name, "index.mdx");
  const hasCover = existsSync(join(draftsRoot, d.name, "cover.jpg"));
  let title = "(no index.mdx)";
  let cat = "";
  if (existsSync(mdx)) {
    const fm = readFrontmatter(readFileSync(mdx, "utf8"));
    title = fmString(fm, "title") || d.name;
    cat = fmString(fm, "category") || "";
  }
  console.log(`  • ${d.name}`);
  console.log(`      ${title}${cat ? `  [${cat}]` : ""}  cover:${hasCover ? "yes" : "NO"}`);
  console.log(`      approve: node agents/scripts/approve.mjs ${d.name}\n`);
}
