import { addLesson, getLessons } from '../storage/lessons-store.js';
/** Handle log_lesson tool call. */
export async function handleLogLesson(input) {
    const { projectPath, projectId, specId, category, severity, title, description, prevention, tags = [], } = input;
    const lesson = await addLesson(projectPath, {
        projectId,
        specId,
        category,
        severity,
        title,
        description,
        prevention,
        tags,
    });
    const lines = [
        `Lesson logged: [${lesson.id}] ${lesson.title}`,
        `Category: ${lesson.category} | Severity: ${lesson.severity}`,
        ...(lesson.specId ? [`Linked spec: ${lesson.specId}`] : []),
        `Prevention: ${lesson.prevention}`,
        ...(lesson.tags.length > 0 ? [`Tags: ${lesson.tags.join(', ')}`] : []),
    ];
    return {
        content: [{ type: 'text', text: lines.join('\n') }],
        structuredContent: { lesson },
    };
}
/** Build category frequency stats from a list of lessons. */
function buildCategoryStats(lessons) {
    const counts = new Map();
    for (const l of lessons) {
        counts.set(l.category, (counts.get(l.category) ?? 0) + 1);
    }
    return Array.from(counts.entries())
        .sort((a, b) => b[1] - a[1])
        .map(([category, count]) => ({ category, count }));
}
/** Handle list_lessons tool call. */
export async function handleListLessons(input) {
    const { projectPath, category, severity, specId, limit = 20 } = input;
    const lessons = await getLessons(projectPath, { category, severity, specId, limit });
    const stats = buildCategoryStats(lessons);
    if (lessons.length === 0) {
        return {
            content: [{ type: 'text', text: 'No lessons found matching the given filters.' }],
            structuredContent: { lessons: [], stats: [] },
        };
    }
    const lines = [
        `Found ${lessons.length} lesson(s):`,
        '',
        ...lessons.map((l) => `[${l.id}] (${l.severity}) ${l.title}\n  Category: ${l.category}` +
            (l.specId ? ` | Spec: ${l.specId}` : '') +
            `\n  Prevention: ${l.prevention}` +
            (l.tags.length > 0 ? `\n  Tags: ${l.tags.join(', ')}` : '')),
        '',
        'Frequency by category:',
        ...stats.map((s) => `  ${s.category}: ${s.count}`),
    ];
    return {
        content: [{ type: 'text', text: lines.join('\n') }],
        structuredContent: { lessons, stats },
    };
}
//# sourceMappingURL=lessons-handler.js.map