export default function auditCommand(pi) {
    pi.registerCommand("audit", {
        description: "Audit the current codebase against a specific goal and write a structured report to .nerf/audits/",
        async handler(args, ctx) {
            // ── Step 1: Get the audit goal ────────────────────────────────────────
            let goal = (typeof args === "string" ? args : "").trim();
            if (!goal) {
                const input = await ctx.ui.input("What is the audit goal?", "e.g. understand performance bottlenecks before planning a roadmap");
                if (!input?.trim()) {
                    ctx.ui.notify("audit: No goal provided — cancelled.", "error");
                    return;
                }
                goal = input.trim();
            }
            // ── Step 2: Build output path (.nerf/audits/<timestamp>-<slug>.md) ────
            const now = new Date();
            const timestamp = now
                .toISOString()
                .replace(/T/, "-")
                .replace(/:/g, "")
                .replace(/\..+/, "");
            const slug = goal
                .toLowerCase()
                .replace(/[^a-z0-9]+/g, "-")
                .replace(/^-+|-+$/g, "")
                .slice(0, 40);
            const outputPath = `.nerf/audits/${timestamp}-${slug}.md`;
            // ── Step 3: Ensure the output directory exists ───────────────────────
            await pi.exec("mkdir", ["-p", ".nerf/audits"]);
            // ── Step 4: Send the audit prompt to the agent ───────────────────────
            const prompt = `You are conducting a codebase audit. This is a **read-only recce** — you will explore the codebase deeply and produce a structured report. You must NOT edit any code or create any files other than the audit report itself.

## Audit Goal
${goal}

## Your Task

1. **Discover the codebase** — explore the project structure, key files, configuration, dependencies, and architecture. Use \`find\`, \`ls\`, \`cat\`, \`grep\`, and \`read\` freely. Read as deeply as needed to form a thorough opinion relative to the goal.

2. **Analyse against the goal** — evaluate the codebase specifically through the lens of the audit goal. What already exists? What works well? What's missing, weak, or risky?

3. **Write the audit report** to \`${outputPath}\` using exactly this markdown template:

\`\`\`markdown
# Audit: ${goal}

**Date:** ${now.toLocaleDateString("en-US", { year: "numeric", month: "long", day: "numeric" })}
**Goal:** ${goal}
**Codebase:** [detected project name / root path]

---

## Strengths
<!-- What already exists that supports this goal? What's solid? -->

## Gaps
<!-- What's missing, incomplete, or problematic relative to this goal? Be specific: file paths, patterns, missing abstractions. -->

## Next Steps
<!-- Concrete, prioritised actions. These should be directly usable as input to /nerf-roadmap. -->

---

*Generated by /audit — read-only recce, no code was modified.*
\`\`\`

After writing the file, confirm with: "✅ Audit complete — report saved to \`${outputPath}\`"`;
            ctx.ui.notify(`Starting audit: "${goal}"`, "info");
            pi.sendUserMessage(prompt);
        },
    });
}
