import type { TriageVerdict } from "@/types/index.js";

/** Labels for Jira / Slack (includes literal RUN_FAILED for operator visibility). */
export function formatReadinessVerdictForHumans(verdict: TriageVerdict): string {
  if (verdict === "NEEDS_INFO") {
    return "Not ready — needs more detail or scope clarity";
  }
  if (verdict === "RUN_FAILED") {
    return "RUN_FAILED — the automated readiness check did not complete; see summary below";
  }
  return "Ready to implement";
}

/**
 * Strips Markdown code fences often pasted from the agent template into SUMMARY / QUESTIONS
 * (avoids orphan ``` lines in Jira comments). Repeats until stable.
 */
const STRIP_FENCE_MAX_PASSES = 20;

export function stripCodeFenceArtifacts(text: string): string {
  let t = text.trim();
  let prev = "";
  let passes = 0;
  while (t !== prev && passes < STRIP_FENCE_MAX_PASSES) {
    prev = t;
    passes++;
    t = t
      .replace(/^\s*```[^\n]*\s*\n?/, "")
      .replace(/\n```\s*$/, "")
      .trim();
  }
  return t;
}
