"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildWorkerPullRequestReviewNecessityContract = buildWorkerPullRequestReviewNecessityContract;
exports.validateWorkerPullRequestReviewNecessitySemanticRules = validateWorkerPullRequestReviewNecessitySemanticRules;
const zod_1 = require("zod");
const structured_contract_support_1 = require("./structured-contract-support");
const types_1 = require("./types");
const validation_issues_1 = require("./validation-issues");
const workerPullRequestReviewNecessitySchema = zod_1.z
    .object({
    action: zod_1.z
        .enum([
        types_1.WORKER_PULL_REQUEST_REVIEW_NECESSITY_ACTIONS.runReview,
        types_1.WORKER_PULL_REQUEST_REVIEW_NECESSITY_ACTIONS.skipReview,
    ])
        .describe("Whether AlpacaLoop should run the PR review workflow."),
    reason: zod_1.z
        .string()
        .trim()
        .min(1)
        .describe("Concise rationale for the review necessity decision."),
    evidence: zod_1.z
        .array(zod_1.z.string().trim().min(1))
        .min(1)
        .describe("Concrete evidence from the PR, linked issue, comments, files, commits, or host state."),
    reviewTriggers: zod_1.z
        .array(zod_1.z.string().trim().min(1))
        .describe("Specific risk signals that require review. Empty only when action is skip_review."),
})
    .strict()
    .describe("Pull request review necessity decision.");
function buildWorkerPullRequestReviewNecessityContract(input) {
    return (0, structured_contract_support_1.buildWorkerStructuredContract)({
        schema: workerPullRequestReviewNecessitySchema,
        example: buildWorkerPullRequestReviewNecessityExample(input),
        rules: buildWorkerPullRequestReviewNecessityRules(),
    });
}
function validateWorkerPullRequestReviewNecessitySemanticRules(decision, _input) {
    const issues = [];
    if (decision.action ===
        types_1.WORKER_PULL_REQUEST_REVIEW_NECESSITY_ACTIONS.runReview &&
        decision.reviewTriggers.length === 0) {
        (0, validation_issues_1.addWorkerSemanticValidationIssue)(issues, {
            path: ["reviewTriggers"],
            message: "reviewTriggers must contain at least one trigger when action is run_review.",
        });
    }
    if (decision.action ===
        types_1.WORKER_PULL_REQUEST_REVIEW_NECESSITY_ACTIONS.skipReview &&
        decision.reviewTriggers.length > 0) {
        (0, validation_issues_1.addWorkerSemanticValidationIssue)(issues, {
            path: ["reviewTriggers"],
            message: "reviewTriggers must be empty when action is skip_review.",
        });
    }
    return issues;
}
function buildWorkerPullRequestReviewNecessityExample(_input) {
    return {
        action: types_1.WORKER_PULL_REQUEST_REVIEW_NECESSITY_ACTIONS.skipReview,
        reason: "The PR is narrow, low risk, and sufficiently evidenced by the linked issue and changed files.",
        evidence: [
            "Changed files are limited to documentation or other low-risk content.",
        ],
        reviewTriggers: [],
    };
}
function buildWorkerPullRequestReviewNecessityRules() {
    return [
        "Return exactly one JSON object in the decision block.",
        "Default to run_review when the risk is unclear, evidence is insufficient, or the PR may affect production behavior.",
        "Choose skip_review only when the PR is narrow, low-risk, clearly satisfies the linked issue, has no unresolved blocker comments, and the available evidence is enough to trust that a separate AI PR review is unlikely to add value.",
        "Choose run_review for auth, permissions, secrets, billing, data deletion, deployments, CI/CD, dependencies, lockfiles, database schema, migrations, background workers, prompts, automation policy, merge policy, production behavior, large or multi-area changes, generated code that is hard to inspect, insufficient tests, unmet issue requirements, unresolved comments, or any uncertainty.",
        "Documentation-only changes can skip review only when they do not change operational instructions, security guidance, public contracts, release process, or automation behavior.",
        "Test-only or refactor-only changes can skip review only when they are isolated, mechanically obvious, and do not alter runtime behavior or coverage assumptions in a risky way.",
        "Do not run review merely because reviewPolicy is if_needed. The action must be based on concrete risk signals.",
        "Do not include full file contents, implementation advice, commands, or a PR review. This run only decides whether the PR review workflow is worth running.",
    ];
}
