// tools/validate-workflow.ts — SPEC-053 MCP tool handler
// Actions: suggest-model | check-git-flow | check-mcp-conventions | check-worktree | full-audit
import { join } from 'node:path';
import { readFile } from 'node:fs/promises';
import { validateGitFlow, auditMcpConventions, suggestModel, suggestModelFromRules, getModelRulesFromCatalog, validateWorktreeBase, validateIntegrationReadiness, } from '../engine/workflow-validator.js';
let catalogCache = null;
async function loadCatalog() {
    if (catalogCache) {
        return catalogCache;
    }
    try {
        const configPath = new URL('../config/workflow-conventions-catalog.json', import.meta.url);
        const raw = await readFile(configPath, 'utf-8');
        catalogCache = JSON.parse(raw);
        return catalogCache;
    }
    catch {
        /* v8 ignore next -- catalog always bundled */
        return null;
    }
}
function text(content) {
    return { content: [{ type: 'text', text: JSON.stringify(content, null, 2) }] };
}
export async function handleValidateWorkflow(args) {
    const projectPath = args.projectPath ?? process.cwd();
    switch (args.action) {
        case 'suggest-model': {
            const taskDesc = args.taskDescription ?? '';
            const catalog = await loadCatalog();
            let result;
            /* v8 ignore start */
            if (!catalog) {
                result = suggestModel(taskDesc);
            }
            else {
                /* v8 ignore stop */
                const rules = getModelRulesFromCatalog(catalog);
                result = suggestModelFromRules(taskDesc, rules);
            }
            return text(result);
        }
        case 'check-git-flow': {
            const result = validateGitFlow(projectPath);
            const summary = result.valid
                ? 'Git flow is clean — no violations detected.'
                : `${result.violations.length} violation(s) found. Review suggestions for each.`;
            return text({ ...result, summary });
        }
        case 'check-mcp-conventions': {
            const toolsDir = args.toolsDir ?? join(projectPath, 'src/tools');
            const result = await auditMcpConventions(toolsDir);
            const summary = result.violations.length === 0
                ? `All ${result.totalChecked} tool file(s) comply with MCP conventions.`
                : `${result.violations.length} violation(s) across ${result.totalChecked} tool file(s).`;
            return text({ ...result, summary });
        }
        case 'check-worktree': {
            const worktreePath = args.worktreePath ?? projectPath;
            const result = validateWorktreeBase(worktreePath);
            return text(result);
        }
        case 'full-audit': {
            const toolsDir = args.toolsDir ?? join(projectPath, 'src/tools');
            const gitFlow = validateGitFlow(projectPath);
            const worktree = validateWorktreeBase(projectPath);
            const integration = validateIntegrationReadiness(projectPath);
            const mcpAudit = await auditMcpConventions(toolsDir);
            const allClear = gitFlow.valid && worktree.valid && integration.ready && mcpAudit.violations.length === 0;
            const summary = allClear
                ? 'All workflow checks passed — ready to commit.'
                : 'One or more workflow checks failed. Review the results below.';
            return text({ allClear, summary, gitFlow, worktree, integration, mcpAudit });
        }
    }
}
//# sourceMappingURL=validate-workflow.js.map