/**
 * api-client/remediation.ts — Error-to-hint matching for common failures.
 *
 * When the pipeline fails remotely, the CLI extracts the structured error
 * (step + message) and matches it against known patterns to provide
 * actionable remediation advice. This replaces the inline JavaScript
 * hints that were previously embedded in the generated workflow YAML.
 *
 * Keeping hints in TypeScript means they're:
 * - Testable (unit tests per pattern)
 * - Version-locked to the CLI (updated automatically)
 * - Not duplicated across workflow templates
 */
const HINTS = [
    {
        match: (e) => /canonical context.*empty/i.test(e.message) ||
            /no article found for slug/i.test(e.message),
        hint: "One or more `context.docs` entries in your task definitions don't " +
            "resolve to any article in the documentation. Check the `slug`, " +
            "`path`, or `id` values in `.ailf/tasks/` and ensure they correspond " +
            "to real articles.\n" +
            "  Run `ailf validate` to check your task definitions locally.",
    },
    {
        match: (e) => /no tasks found/i.test(e.message),
        hint: "No evaluation tasks were found. Add task YAML files to `.ailf/tasks/`.\n" +
            "  Run `ailf init` to generate example tasks.",
    },
    {
        match: (e) => /rate_limit_exceeded/i.test(e.message),
        hint: "Your daily evaluation budget has been reached. Try again tomorrow,\n" +
            "  or use `--debug` to run a smaller subset of tests.",
    },
    {
        match: (e) => /concurrent_limit_exceeded/i.test(e.message),
        hint: "Too many evaluations are running simultaneously.\n" +
            "  Wait for the current jobs to finish before submitting a new one.",
    },
    {
        match: (e) => /authentication_failed/i.test(e.message) ||
            /invalid.*api.?key/i.test(e.message),
        hint: "Check that `AILF_API_KEY` is set correctly in your environment.\n" +
            "  The key should start with `ailf_live_sk_` or `ailf_test_sk_`.",
    },
    {
        match: (e) => /validation_error/i.test(e.message) && /inlineTasks/i.test(e.message),
        hint: "Your task definitions have validation errors.\n" +
            "  Run `ailf validate` locally to see detailed error messages.",
    },
    {
        match: (e) => e.step === "fetch-docs" && /postcondition/i.test(e.message),
        hint: "The documentation fetch step completed but one or more tasks had " +
            "empty context. This usually means a `context.docs` entry doesn't " +
            "resolve to any article.\n" +
            "  Check the slug/path/id values in `.ailf/tasks/`.",
    },
    {
        match: (e) => e.step === "dispatch" && /dispatch failed/i.test(e.message),
        hint: "The evaluation could not be dispatched to the execution backend.\n" +
            "  This is usually a transient infrastructure issue — try again in a few minutes.",
    },
];
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
 * Match an error against known patterns and return a remediation hint,
 * or `undefined` if no pattern matches.
 */
export function getRemediationHint(error) {
    for (const entry of HINTS) {
        if (entry.match(error)) {
            return entry.hint;
        }
    }
    return undefined;
}
