// tools/generate-annotations.ts — Suggest @spec annotations for unannotated files
// Uses living-spec-analyzer heuristics to match files to spec criteria.
import { specStore, knowledgeStore } from '../storage/index.js';
import { parseAnnotationsFromProject } from '../engine/living-spec/index.js';
import { analyzeLivingSpec } from '../engine/living-spec-analyzer.js';
export async function handleGenerateAnnotations(args) {
    const { projectId, projectPath, specId } = args;
    // 1. Load spec
    const spec = await specStore.getSpec(projectId, specId);
    if (!spec) {
        return {
            content: [{ type: 'text', text: `Spec ${specId} not found` }],
            isError: true,
        };
    }
    // 2. Get already-annotated files
    const parseResult = await parseAnnotationsFromProject(projectPath, specId);
    const annotatedFiles = new Set(parseResult.annotations.map((a) => a.file));
    // 3. Run living-spec analysis to find matched files
    const knowledge = await knowledgeStore.getKnowledge(projectId);
    const analysisPath = knowledge?.projectPath ?? projectPath;
    const report = await analyzeLivingSpec(spec, analysisPath);
    // 4. Generate suggestions for files that matched criteria but lack annotations
    const suggestions = [];
    for (const assessment of report.criteria) {
        if (assessment.status === 'met' && assessment.matchedFile && !annotatedFiles.has(assessment.matchedFile)) {
            // Find the criterion index (1-based)
            const criterionIndex = report.criteria.indexOf(assessment) + 1;
            suggestions.push({
                file: assessment.matchedFile,
                suggestedAnnotation: `// @spec ${specId}:AC-${String(criterionIndex)} — ${truncate(assessment.criterion, 60)}`,
                reason: assessment.evidence,
                confidence: determineConfidence(assessment.evidence),
            });
        }
    }
    return {
        content: [
            {
                type: 'text',
                text: JSON.stringify({
                    specId,
                    totalSuggestions: suggestions.length,
                    suggestions,
                    alreadyAnnotated: annotatedFiles.size,
                    message: suggestions.length > 0
                        ? `${String(suggestions.length)} annotation suggestion(s) generated. Add these to your source files.`
                        : 'No unannotated files found that match spec criteria.',
                }, null, 2),
            },
        ],
    };
}
function truncate(text, maxLen) {
    if (text.length <= maxLen) {
        return text;
    }
    return text.slice(0, maxLen - 3) + '...';
}
function determineConfidence(evidence) {
    const lower = evidence.toLowerCase();
    if (lower.includes('file exists') || lower.includes('symbol')) {
        return 'high';
    }
    if (lower.includes('route') || lower.includes('test file')) {
        return 'medium';
    }
    return 'low';
}
//# sourceMappingURL=generate-annotations.js.map