// tools/convention-sync.ts — MCP tool: config_health
// Shows overall sync percentage between project conventions and their enforcement.
// READ-ONLY: never modifies files.
import { z } from 'zod';
import { calculateConventionSync } from '../engine/convention-sync.js';
import { getKnowledge } from '../storage/knowledge-store.js';
import { safeTracked } from './safe-handler.js';
// ---------------------------------------------------------------------------
// Zod schema
// ---------------------------------------------------------------------------
const configHealthSchema = {
    projectId: z
        .string()
        .max(64)
        .describe('Project identifier (hash) used to load project knowledge for skills analysis'),
    projectPath: z.string().max(4096).describe('Absolute path to the project root to analyse'),
};
// ---------------------------------------------------------------------------
// Formatter
// ---------------------------------------------------------------------------
function scoreBar(score) {
    const filled = Math.round(score / 10);
    return '[' + '█'.repeat(filled) + '░'.repeat(10 - filled) + ']';
}
function formatReport(report) {
    const { overallScore, rulesHealth, skillsHealth, hooksHealth, conventionHealth } = report;
    const lines = [];
    lines.push('# Convention Sync Health Report');
    lines.push('');
    lines.push(`## Overall Score: ${overallScore}/100  ${scoreBar(overallScore)}`);
    lines.push('');
    lines.push(`**${report.summary}**`);
    lines.push('');
    lines.push('## Dimension Breakdown');
    lines.push('');
    lines.push(`| Dimension       | Score | Bar                        |`);
    lines.push(`|-----------------|-------|----------------------------|`);
    lines.push(`| Rules files     | ${String(rulesHealth).padStart(3)}/100 | ${scoreBar(rulesHealth)} |`);
    lines.push(`| Skills          | ${String(skillsHealth).padStart(3)}/100 | ${scoreBar(skillsHealth)} |`);
    lines.push(`| Git hooks       | ${String(hooksHealth).padStart(3)}/100 | ${scoreBar(hooksHealth)} |`);
    lines.push(`| Conventions     | ${String(conventionHealth).padStart(3)}/100 | ${scoreBar(conventionHealth)} |`);
    lines.push('');
    if (report.recommendations.length > 0) {
        lines.push('## Recommendations');
        lines.push('');
        for (const rec of report.recommendations) {
            lines.push(`- ${rec}`);
        }
        lines.push('');
    }
    return lines.join('\n');
}
// ---------------------------------------------------------------------------
// Handler
// ---------------------------------------------------------------------------
export async function handleConfigHealth(args) {
    const knowledge = await getKnowledge(args.projectId);
    const report = await calculateConventionSync(args.projectPath, knowledge);
    const text = formatReport(report);
    const isError = report.overallScore === 0;
    return { content: [{ type: 'text', text }], isError };
}
// ---------------------------------------------------------------------------
// Registration
// ---------------------------------------------------------------------------
export function registerConfigHealthTool(server) {
    server.registerTool('config_health', {
        description: "Calculate the overall sync percentage between the project's conventions and its enforcement mechanisms (rules files, skills, git hooks, convention violations). Returns a 0–100 weighted score with per-dimension breakdown and prioritised recommendations. READ-ONLY — never modifies files.",
        inputSchema: configHealthSchema,
        annotations: {
            readOnlyHint: true,
            destructiveHint: false,
            title: 'Config Health',
        },
    }, safeTracked('config_health', async (args) => handleConfigHealth(args)));
}
//# sourceMappingURL=convention-sync.js.map