// tools/reconcile-hooks.ts — MCP tool: reconcile_hooks
// Detects drift between CI/quality pipeline and git hooks.
// With autoFix:true, patches husky hook files with missing quality checks.
import { z } from 'zod';
import { reconcileHooks, patchHooks } from '../engine/hooks-reconciler.js';
import { safeTracked } from './safe-handler.js';
// ---------------------------------------------------------------------------
// Zod schema
// ---------------------------------------------------------------------------
const reconcileHooksSchema = {
    projectPath: z
        .string()
        .max(4096)
        .describe('Absolute path to the project root to analyse git hooks against CI capabilities'),
    autoFix: z
        .boolean()
        .optional()
        .describe('When true, automatically patches husky hook files to add missing quality checks. ' +
        'Idempotent: checks already present are skipped. Default: false.'),
};
// ---------------------------------------------------------------------------
// Formatter
// ---------------------------------------------------------------------------
function formatReconciliation(result) {
    const { hooksInSync, missingHooks, outdatedHooks, healthScore } = result;
    const lines = [];
    lines.push('# Hooks Reconciliation Report');
    lines.push('');
    lines.push(`## Health Score: ${healthScore}/100`);
    lines.push('');
    lines.push(`- Status: **${hooksInSync ? 'IN SYNC' : 'DRIFT DETECTED'}**`);
    lines.push('');
    if (missingHooks.length > 0) {
        lines.push('## Missing Checks');
        lines.push('');
        lines.push('Expected quality gates not found in current hook scripts:');
        lines.push('');
        for (const item of missingHooks) {
            lines.push(`  - ${item}`);
        }
        lines.push('');
        lines.push('Run `configure_hooks` or `manage_git setup-hooks` to add missing steps.');
        lines.push('');
    }
    if (outdatedHooks.length > 0) {
        lines.push('## Enhancement Opportunities');
        lines.push('');
        lines.push('Hooks could be improved based on detected project capabilities:');
        lines.push('');
        for (const item of outdatedHooks) {
            lines.push(`  - ${item}`);
        }
        lines.push('');
    }
    if (hooksInSync) {
        lines.push('All detected project capabilities are covered by git hook scripts. No drift found.');
        lines.push('');
    }
    return lines.join('\n');
}
function formatPatchResult(reconcileResult, patchResult) {
    const lines = [];
    lines.push('# Hooks Reconciliation Report (Auto-Fix Applied)');
    lines.push('');
    lines.push(`## Health Score: ${patchResult.healthScore}/100`);
    lines.push('');
    lines.push(`- Status: **${reconcileResult.hooksInSync ? 'IN SYNC' : 'DRIFT DETECTED (before fix)'}**`);
    lines.push('');
    if (patchResult.patched.length > 0) {
        lines.push('## Patched Checks');
        lines.push('');
        for (const p of patchResult.patched) {
            lines.push(`  - ${p.hook}: \`${p.added}\``);
        }
        lines.push('');
    }
    if (patchResult.skipped.length > 0) {
        lines.push('## Skipped (already present or covered by lint-staged)');
        lines.push('');
        for (const s of patchResult.skipped) {
            lines.push(`  - ${s}`);
        }
        lines.push('');
    }
    if (patchResult.patched.length === 0 && patchResult.skipped.length === 0) {
        lines.push('No patches needed — hooks are already up to date.');
        lines.push('');
    }
    if (reconcileResult.outdatedHooks.length > 0) {
        lines.push('## Enhancement Opportunities (not auto-fixed)');
        lines.push('');
        for (const item of reconcileResult.outdatedHooks) {
            lines.push(`  - ${item}`);
        }
        lines.push('');
    }
    return lines.join('\n');
}
// ---------------------------------------------------------------------------
// Handler
// ---------------------------------------------------------------------------
export function handleReconcileHooks(args) {
    const result = reconcileHooks(args.projectPath);
    const text = formatReconciliation(result);
    const isError = result.healthScore === 0 && !result.hooksInSync;
    return { content: [{ type: 'text', text }], isError };
}
export function handleReconcileHooksAsync(args) {
    const result = reconcileHooks(args.projectPath);
    if (!args.autoFix) {
        const text = formatReconciliation(result);
        const isError = result.healthScore === 0 && !result.hooksInSync;
        return { content: [{ type: 'text', text }], isError };
    }
    const patch = patchHooks(result.missingHooks, args.projectPath);
    const text = formatPatchResult(result, patch);
    const isError = patch.healthScore === 0 && patch.patched.length === 0;
    return { content: [{ type: 'text', text }], isError };
}
// ---------------------------------------------------------------------------
// Registration
// ---------------------------------------------------------------------------
export function registerReconcileHooksTool(server) {
    server.registerTool('reconcile_hooks', {
        description: 'Detect drift between the project CI/quality pipeline and its git hooks. Checks whether hooks enforce TypeScript, ESLint, tests, commitlint, and Prettier based on what the project has installed. Returns a health score (0-100) and lists missing or outdated steps. ' +
            'With autoFix:true, automatically patches husky hook files to add missing quality checks (idempotent).',
        inputSchema: reconcileHooksSchema,
        annotations: {
            readOnlyHint: false,
            destructiveHint: false,
            title: 'Reconcile Hooks',
        },
    }, safeTracked('reconcile_hooks', (args) => Promise.resolve(handleReconcileHooksAsync(args))));
}
//# sourceMappingURL=reconcile-hooks.js.map