// tools/doc-compliance-handler.ts — SPEC-385
// Handler for the doc_compliance_report tool.
import { join } from 'node:path';
import { hashProjectPath, specStore } from '../storage/index.js';
import { checkDocCompliance } from '../engine/doc-compliance-checker.js';
export async function handleDocComplianceReport(input) {
    const { projectPath, specId } = input;
    const projectId = hashProjectPath(projectPath);
    // Load spec to get specPath
    const specs = await specStore.listSpecs(projectId);
    const spec = specs.find((s) => s.id === specId);
    let specPath;
    if (spec?.specPath) {
        specPath = spec.specPath;
    }
    else {
        // Fallback: try to find spec.md in the standard location
        specPath = join(projectPath, 'planu', 'specs', specId, 'spec.md');
    }
    const report = await checkDocCompliance(specPath, specId);
    // Format output
    const issueLines = [];
    for (const issue of report.issues) {
        const icon = issue.severity === 'high' ? '🔴' : issue.severity === 'medium' ? '🟡' : '🟢';
        issueLines.push(`${icon} [${issue.tech}] ${issue.issueType}: ${issue.description}`);
        if (issue.source) {
            issueLines.push(`   Source: ${issue.source}`);
        }
    }
    const coveredLines = report.bestPracticesCovered.slice(0, 5).map((bp) => `✅ ${bp}`);
    const text = [
        `## Doc Compliance Report — ${report.specId}`,
        `**Title**: ${report.specTitle}`,
        `**Score**: ${report.complianceScore}/100`,
        `**Technologies**: ${report.techs.join(', ') || 'none detected'}`,
        `**Checked**: ${report.checkedAt}`,
        '',
        `### Summary`,
        report.summary,
        '',
        ...(issueLines.length > 0
            ? ['### Issues Found', ...issueLines, '']
            : ['### No issues found ✅', '']),
        ...(coveredLines.length > 0 ? ['### Best Practices Covered', ...coveredLines] : []),
    ].join('\n');
    return { content: [{ type: 'text', text }] };
}
//# sourceMappingURL=doc-compliance-handler.js.map