// tools/reconcile-skills.ts — SPEC-189: reconcile_skills MCP tool registration
// SPEC-395: adds autoFix:boolean to auto-install missing, remove stale, repair paths.
import { safeTracked } from './safe-handler.js';
import { ReconcileSkillsInputSchema } from './schemas/reconcile-skills-schemas.js';
import { reconcileSkills, autoFixSkills } from '../engine/skills-reconciler.js';
import { sanitizePath } from './sanitize-path.js';
import { knowledgeStore } from '../storage/index.js';
// ---------------------------------------------------------------------------
// Handler
// ---------------------------------------------------------------------------
export async function handleReconcileSkills(args) {
    const projectPath = sanitizePath(args.projectPath);
    const knowledge = await knowledgeStore.getKnowledge(args.projectId);
    const reconciliation = await reconcileSkills(projectPath, knowledge);
    if (args.autoFix === true) {
        const fixResult = await autoFixSkills(reconciliation, projectPath);
        return buildAutoFixReport(fixResult);
    }
    return buildReadOnlyReport(reconciliation);
}
// ---------------------------------------------------------------------------
// Report builders
// ---------------------------------------------------------------------------
function buildReadOnlyReport(result) {
    const lines = [];
    lines.push(`## Skills Reconciliation Report`);
    lines.push(`**Health score**: ${result.healthScore}/100`);
    lines.push(`**In sync**: ${result.skillsInSync ? 'Yes' : 'No'}`);
    lines.push('');
    if (result.newSkillsDetected.length > 0) {
        lines.push(`### New skills detected (${result.newSkillsDetected.length})`);
        lines.push('These skills are recommended for your current stack but are not installed:');
        for (const name of result.newSkillsDetected) {
            lines.push(`- \`${name}\``);
        }
        lines.push('');
        lines.push('Run `skill_install` for each skill, or use `suggest_tooling` for full details.');
    }
    else {
        lines.push('All recommended skills are installed.');
    }
    lines.push('');
    if (result.staleSkills.length > 0) {
        lines.push(`### Stale skills (${result.staleSkills.length})`);
        lines.push('These skills are installed for technologies no longer detected in the stack:');
        for (const name of result.staleSkills) {
            lines.push(`- \`${name}\``);
        }
        lines.push('');
        lines.push('Consider removing them from `.claude/skills/` if they are no longer needed.');
    }
    else {
        lines.push('No stale skills detected.');
    }
    return { content: [{ type: 'text', text: lines.join('\n') }] };
}
function buildAutoFixReport(fix) {
    const lines = [];
    lines.push('## Skills Auto-Fix Report');
    lines.push(`**Health score after fix**: ${fix.healthScore}/100`);
    lines.push('');
    if (fix.installed.length > 0) {
        lines.push(`### Installed (${fix.installed.length})`);
        for (const name of fix.installed) {
            lines.push(`- \`${name}\``);
        }
        lines.push('');
    }
    else {
        lines.push('No missing skills to install.');
        lines.push('');
    }
    if (fix.removed.length > 0) {
        lines.push(`### Removed (${fix.removed.length})`);
        for (const name of fix.removed) {
            lines.push(`- \`${name}\``);
        }
        lines.push('');
    }
    else {
        lines.push('No stale skills to remove.');
        lines.push('');
    }
    if (fix.pathsFixed.length > 0) {
        lines.push(`### Paths corrected (${fix.pathsFixed.length})`);
        for (const name of fix.pathsFixed) {
            lines.push(`- \`${name}\``);
        }
        lines.push('');
    }
    else {
        lines.push('No corrupt manifest paths detected.');
    }
    return { content: [{ type: 'text', text: lines.join('\n') }] };
}
// ---------------------------------------------------------------------------
// Registration
// ---------------------------------------------------------------------------
export function registerReconcileSkillsTool(server) {
    server.registerTool('reconcile_skills', {
        description: "Analyze the gap between the project's current tech stack and its installed Claude skills. " +
            'Detects skills that are recommended for the stack but not yet installed (newSkillsDetected) ' +
            'and skills installed for technologies no longer in use (staleSkills). ' +
            'Returns a health score (0–100) indicating how well skills match the current stack. ' +
            'Pass autoFix:true to automatically install missing skills, remove stale skills, ' +
            'and repair corrupt manifest paths (mutates .claude/skills/).',
        inputSchema: {
            projectId: ReconcileSkillsInputSchema.shape.projectId,
            projectPath: ReconcileSkillsInputSchema.shape.projectPath,
            autoFix: ReconcileSkillsInputSchema.shape.autoFix,
        },
        annotations: {
            title: 'Reconcile Skills',
            readOnlyHint: false,
        },
    }, safeTracked('reconcile_skills', async (args) => handleReconcileSkills(args)));
}
//# sourceMappingURL=reconcile-skills.js.map