#!/usr/bin/env node

import { appendFileSync, existsSync, mkdirSync, writeFileSync } from "node:fs";
import { join, resolve } from "node:path";

function usage() {
  console.error(
    [
      "Usage:",
      "  node evals/capture-failure-case.mjs --id <id> --prompt <text> [--should-trigger true|false] [--category <name>] [--notes <text>]",
    ].join("\n")
  );
}

function getArg(name, fallback = null) {
  const index = process.argv.indexOf(name);
  if (index === -1 || index === process.argv.length - 1) return fallback;
  return process.argv[index + 1];
}

const id = getArg("--id");
const prompt = getArg("--prompt");
const shouldTrigger = getArg("--should-trigger", "true");
const category = getArg("--category", "captured-failure");
const notes = getArg("--notes", "");

if (!id || !prompt) {
  usage();
  process.exit(1);
}

const repoRoot = process.cwd();
const promptsCsvPath = join(repoRoot, "evals", "prompts.csv");
const failuresDir = join(repoRoot, "evals", "failure-cases");
const failureJsonPath = join(failuresDir, `${id}.json`);

if (!existsSync(promptsCsvPath)) {
  console.error(`prompts.csv not found at ${promptsCsvPath}`);
  process.exit(1);
}

mkdirSync(failuresDir, { recursive: true });

const escapedPrompt = `"${prompt.replace(/"/g, '""')}"`;
appendFileSync(promptsCsvPath, `\n${id},${shouldTrigger},${category},${escapedPrompt}\n`);

writeFileSync(
  failureJsonPath,
  JSON.stringify(
    {
      id,
      should_trigger: shouldTrigger === "true",
      category,
      prompt,
      notes,
      captured_at: new Date().toISOString(),
    },
    null,
    2
  ) + "\n"
);

console.log(
  JSON.stringify(
    {
      captured: true,
      prompt_csv: resolve(promptsCsvPath),
      failure_case: resolve(failureJsonPath),
    },
    null,
    2
  )
);
