/**
 * Centralized verdict extraction, normalization, and schema validation.
 *
 * All verdict-related logic lives here so that normalization rules
 * (e.g. `passed` → `pass`) are applied consistently across the codebase.
 */
import { extractUatType } from "./files.js";
// ── Verdict extraction ──────────────────────────────────────────────────
/**
 * Extract and normalize the `verdict` value from YAML frontmatter.
 *
 * Normalization:
 * - lowercased
 * - `passed` → `pass`
 *
 * Returns `undefined` when frontmatter is absent or has no `verdict` field.
 */
export function extractVerdict(content) {
	// Strip leading generated-file comment header before parsing frontmatter.
	// VALIDATION.md is a DB projection; the header is always the first line.
	const stripped = content.startsWith("<!--")
		? content.replace(/^<!--[^\n]*-->\n/, "")
		: content;
	// Primary: YAML frontmatter verdict (canonical format)
	const fmMatch = stripped.match(/^---\n([\s\S]*?)\n---/);
	if (fmMatch) {
		const verdictMatch = fmMatch[1].match(/verdict:\s*([\w-]+)/i);
		if (verdictMatch) {
			let v = verdictMatch[1].toLowerCase();
			if (v === "passed") v = "pass";
			return v;
		}
		return undefined;
	}
	// Fallback: detect verdict in markdown body (LLM manual writes, #2960).
	// Matches patterns like: **Verdict:** PASS, **Verdict:** ✅ PASS, **Verdict** needs-remediation
	const bodyMatch = stripped.match(/\*\*Verdict:?\*\*\s*(?:✅\s*)?(\w[\w-]*)/i);
	if (bodyMatch) {
		let v = bodyMatch[1].toLowerCase();
		if (v === "passed") v = "pass";
		return v;
	}
	return undefined;
}
/**
 * Returns `true` when the content's frontmatter contains a `verdict` field.
 */
export function hasVerdict(content) {
	return /verdict:\s*[\w-]+/i.test(content);
}
// ── UAT verdict schema ──────────────────────────────────────────────────
/**
 * Base verdicts that are always acceptable for UAT results.
 */
export const UAT_ACCEPTABLE_VERDICTS = ["pass", "passed"];
/**
 * UAT types whose results may legitimately produce a `partial` verdict
 * when all automatable checks pass but human-only checks remain.
 */
const PARTIAL_ELIGIBLE_UAT_TYPES = [
	"mixed",
	"human-experience",
	"live-runtime",
];
/**
 * Check whether a verdict is acceptable for a given UAT type.
 *
 * `pass` / `passed` are always acceptable. `partial` is acceptable only for
 * UAT types that include non-automatable human checks.
 */
export function isAcceptableUatVerdict(verdict, uatType) {
	if (UAT_ACCEPTABLE_VERDICTS.includes(verdict)) return true;
	if (
		verdict === "partial" &&
		uatType &&
		PARTIAL_ELIGIBLE_UAT_TYPES.includes(uatType)
	) {
		return true;
	}
	return false;
}
// ── Milestone validation verdict schema ─────────────────────────────────
/**
 * Valid verdicts for the `validate-milestone` tool.
 */
export const VALIDATION_VERDICTS = [
	"pass",
	"needs-attention",
	"needs-remediation",
];
/**
 * Check whether a string is a valid milestone validation verdict.
 */
export function isValidMilestoneVerdict(verdict) {
	return VALIDATION_VERDICTS.includes(verdict);
}
// ── UAT type helper ─────────────────────────────────────────────────────
/**
 * Extract the UAT type from content, defaulting to `"artifact-driven"`.
 *
 * The `"artifact-driven"` fallback is the original default used throughout
 * the codebase when a UAT file lacks an explicit `## UAT Type` section.
 */
export function getUatType(content) {
	return extractUatType(content) ?? "artifact-driven";
}
