"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateMemoryDigest = generateMemoryDigest;
const database_1 = require("../database/database");
function generateMemoryDigest(project) {
    const db = (0, database_1.getDb)();
    const totalRow = db.prepare('SELECT COUNT(*) as c FROM memory_events WHERE project = ?').get(project);
    const since = new Date(Date.now() - 7 * 86400000).toISOString();
    const recentRow = db.prepare('SELECT COUNT(*) as c FROM memory_events WHERE project = ? AND created_at >= ?').get(project, since);
    const typeRows = db.prepare(`
    SELECT type, COUNT(*) as c FROM memory_events WHERE project = ? GROUP BY type
  `).all(project);
    const topicRows = db.prepare(`
    SELECT topic, COUNT(*) as c FROM memory_events
    WHERE project = ? AND topic IS NOT NULL
    GROUP BY topic ORDER BY c DESC LIMIT 5
  `).all(project);
    const dayRow = db.prepare(`
    SELECT date(created_at) as d, COUNT(*) as c FROM memory_events
    WHERE project = ? GROUP BY d ORDER BY c DESC LIMIT 1
  `).get(project);
    const map = {};
    for (const r of typeRows)
        map[r.type] = r.c;
    const digestLines = [
        `Total memories: ${totalRow.c}`,
        `Last 7d: ${recentRow.c}`,
        `Decisions: ${map.decision || 0}`,
        `Errors/Fixes: ${map.error || 0}/${map.fix || 0}`,
        `Top topics: ${topicRows.map((t) => t.topic).join(', ') || 'none'}`,
    ];
    return {
        project,
        totalEvents: totalRow.c,
        last7d: recentRow.c,
        decisions: map.decision || 0,
        errors: map.error || 0,
        fixes: map.fix || 0,
        topTopics: topicRows.map((t) => t.topic),
        mostActiveDay: dayRow?.d || null,
        digestLines,
    };
}
//# sourceMappingURL=memory-digest.js.map