// tools/check-config-health.ts — MCP tool handler for check_config_health (SPEC-042)
// READ-ONLY: this tool never modifies files.
import { z } from 'zod';
import { runConfigHealthChecks } from '../engine/config-health/index.js';
// ---------------------------------------------------------------------------
// Zod schema
// ---------------------------------------------------------------------------
const checkConfigHealthSchema = {
    projectPath: z.string().describe('Absolute path to the project root to check'),
    checks: z
        .array(z.enum(['ts', 'python', 'go', 'rust', 'ci', 'build-tool']))
        .optional()
        .describe('Subset of check groups to run. Default: all groups (ts, python, go, rust, ci, build-tool)'),
    severity: z
        .enum(['error', 'warning', 'info'])
        .optional()
        .describe('Minimum severity level to include in the report. Default: info (shows everything)'),
};
// ---------------------------------------------------------------------------
// Formatter
// ---------------------------------------------------------------------------
function formatFinding(finding) {
    const linePart = finding.line !== undefined ? `:${finding.line}` : '';
    const severityLabel = finding.severity === 'error' ? 'ERROR' : finding.severity === 'warning' ? 'WARN' : 'INFO';
    return [
        `  [${severityLabel}] ${finding.file}${linePart}`,
        `    ${finding.message}`,
        `    Fix: ${finding.fix}`,
    ].join('\n');
}
function formatReport(report, projectPath) {
    const { summary, findings, blocksDoR } = report;
    const lines = [];
    lines.push(`# Config Health Report — ${projectPath}`);
    lines.push('');
    // Summary row
    lines.push('## Summary');
    lines.push('');
    lines.push(`- Errors: ${summary.errors}  |  Warnings: ${summary.warnings}  |  Info: ${summary.info}`);
    lines.push(`- Blocks Definition of Ready: **${blocksDoR ? 'YES — fix errors before implementation' : 'NO'}**`);
    lines.push('');
    if (findings.length === 0) {
        lines.push('No config health issues found.');
        return lines.join('\n');
    }
    // Group by checker
    const byChecker = new Map();
    for (const finding of findings) {
        const group = byChecker.get(finding.checker) ?? [];
        group.push(finding);
        byChecker.set(finding.checker, group);
    }
    lines.push('## Findings');
    lines.push('');
    for (const [checker, checkerFindings] of byChecker) {
        lines.push(`### ${checker}`);
        lines.push('');
        for (const f of checkerFindings) {
            lines.push(formatFinding(f));
            lines.push('');
        }
    }
    return lines.join('\n');
}
// ---------------------------------------------------------------------------
// Handler
// ---------------------------------------------------------------------------
export async function handleCheckConfigHealth(args) {
    const checks = args.checks;
    const severity = args.severity ?? 'info';
    const report = await runConfigHealthChecks(args.projectPath, checks, severity);
    const text = formatReport(report, args.projectPath);
    const isError = report.blocksDoR;
    return { content: [{ type: 'text', text }], isError };
}
// ---------------------------------------------------------------------------
// Registration
// ---------------------------------------------------------------------------
export function registerCheckConfigHealthTool(server) {
    server.registerTool('check_config_health', {
        description: 'Scan project configuration files (tsconfig, vitest/jest config, go.mod, Cargo.toml, CI workflows, etc.) for misconfigurations, deprecated options, and cross-file inconsistencies. READ-ONLY — never modifies files.',
        inputSchema: checkConfigHealthSchema,
        annotations: {
            readOnlyHint: true,
            destructiveHint: false,
            title: 'Check Config Health',
        },
    }, async (args) => handleCheckConfigHealth(args));
}
//# sourceMappingURL=check-config-health.js.map