import { z } from 'zod';
import { safeLicensed } from './safe-handler.js';
import { assessMergeRisk } from '../engine/merge-risk-assessor/index.js';
// ── Zod schema ────────────────────────────────────────────────────────────────
const AssessMergeRiskInputSchema = {
    projectPath: z.string().describe('Absolute path to project root'),
    branch: z.string().optional().describe('Branch to analyze (default: current branch from git)'),
    baseBranch: z
        .string()
        .optional()
        .default('main')
        .describe('Base branch to compare against (default: main)'),
    context: z
        .string()
        .optional()
        .describe('Description of the change for spec compliance check. Include a SPEC-NNN ID for best results.'),
};
// ── Formatting ────────────────────────────────────────────────────────────────
function formatRiskResult(result) {
    const recommendationLabel = result.recommendation === 'auto-merge'
        ? 'AUTO-MERGE'
        : result.recommendation === 'human-review'
            ? 'HUMAN REVIEW REQUIRED'
            : 'BLOCK';
    const lines = [
        `Merge Risk Assessment`,
        `=====================`,
        `Risk Score: ${result.riskScore}/100`,
        `Recommendation: ${recommendationLabel}`,
        ``,
        `Summary: ${result.humanSummary}`,
        ``,
        `Diff Stats`,
        `----------`,
        `Files changed: ${result.filesChanged}`,
        `Lines added:   ${result.linesAdded}`,
        `Lines removed: ${result.linesRemoved}`,
        ``,
        `Risk Breakdown`,
        `--------------`,
    ];
    for (const b of result.breakdown) {
        const contribution = Math.round(b.score * b.weight);
        lines.push(`${b.category} (weight ${Math.round(b.weight * 100)}%): ${b.score}/100 [+${contribution} to total]`);
        lines.push(`  ${b.reason}`);
    }
    return lines.join('\n');
}
// ── Handler ───────────────────────────────────────────────────────────────────
async function handleAssessMergeRisk(args) {
    const result = await assessMergeRisk(args.projectPath, {
        branch: args.branch,
        baseBranch: args.baseBranch,
        context: args.context,
    });
    return {
        content: [
            {
                type: 'text',
                text: formatRiskResult(result),
            },
        ],
    };
}
// ── Registration ──────────────────────────────────────────────────────────────
export function registerMergeRiskTools(server) {
    server.registerTool('assess_merge_risk', {
        description: 'Analyze a git branch diff and return a risk score (0-100) with category breakdown ' +
            'and merge recommendation. Evaluates security surface, test coverage, blast radius, ' +
            'reversibility, and spec compliance.',
        inputSchema: AssessMergeRiskInputSchema,
        annotations: {
            title: 'Assess Merge Risk',
            readOnlyHint: true,
            destructiveHint: false,
            openWorldHint: false,
        },
    }, safeLicensed('assess_merge_risk', async (args) => handleAssessMergeRisk(args)));
}
//# sourceMappingURL=merge-risk-handler.js.map