"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.renderActivityChart = renderActivityChart;
exports.renderTypeDistribution = renderTypeDistribution;
exports.renderImportanceChart = renderImportanceChart;
exports.renderTimelineChart = renderTimelineChart;
exports.renderFullDashboard = renderFullDashboard;
const database_1 = require("../database/database");
function renderActivityChart(project, days = 14) {
    const db = (0, database_1.getDb)();
    const since = new Date(Date.now() - days * 86400000).toISOString().slice(0, 10);
    const rows = db.prepare(`
    SELECT date(created_at) as day, COUNT(*) as c
    FROM memory_events
    WHERE project = ? AND date(created_at) >= date(?)
    GROUP BY day ORDER BY day
  `).all(project, since);
    const rowMap = new Map(rows.map(r => [r.day, r.c]));
    const lines = [];
    lines.push(`  ── Activity Chart (${days} days) ──`);
    lines.push('');
    const maxCount = Math.max(...rows.map(r => r.c), 1);
    const barWidth = 30;
    for (let i = days - 1; i >= 0; i--) {
        const d = new Date(Date.now() - i * 86400000).toISOString().slice(0, 10);
        const count = rowMap.get(d) || 0;
        const barLen = Math.round((count / maxCount) * barWidth);
        const bar = '█'.repeat(barLen) + '░'.repeat(barWidth - barLen);
        const dayShort = d.slice(5); // MM-DD
        lines.push(`  ${dayShort} ${bar} ${count}`);
    }
    lines.push('');
    const total = rows.reduce((s, r) => s + r.c, 0);
    const avg = rows.length > 0 ? Math.round(total / rows.length * 10) / 10 : 0;
    lines.push(`  Total: ${total} | Avg/day: ${avg} | Max: ${maxCount}`);
    return lines.join('\n');
}
function renderTypeDistribution(project) {
    const db = (0, database_1.getDb)();
    const rows = db.prepare(`
    SELECT type, COUNT(*) as c FROM memory_events
    WHERE project = ?
    GROUP BY type ORDER BY c DESC
  `).all(project);
    const lines = [];
    lines.push('  ── Event Type Distribution ──');
    lines.push('');
    const maxCount = Math.max(...rows.map(r => r.c), 1);
    const barWidth = 25;
    const colors = {
        decision: '🟢', error: '🔴', edit: '🔵', fix: '🟡',
        file_event: '⚪', publish: '🟣', handoff: '🟠', pattern: '⭐',
        verification: '✅', risk: '⚠️', question: '❓', session: '📋',
        branch: '🌿', style: '🎨', ai_work: '🤖',
    };
    for (const row of rows) {
        const barLen = Math.round((row.c / maxCount) * barWidth);
        const bar = '█'.repeat(barLen);
        const icon = colors[row.type] || '📊';
        lines.push(`  ${icon} ${row.type.padEnd(14)} ${bar} ${row.c}`);
    }
    lines.push('');
    lines.push(`  Total: ${rows.reduce((s, r) => s + r.c, 0)} events`);
    return lines.join('\n');
}
function renderImportanceChart(project) {
    const db = (0, database_1.getDb)();
    const rows = db.prepare(`
    SELECT
      CASE
        WHEN importance >= 8 THEN '8-10 (Critical)'
        WHEN importance >= 5 THEN '5-7 (Medium)'
        WHEN importance >= 3 THEN '3-4 (Low)'
        ELSE '1-2 (Minimal)'
      END as tier,
      COUNT(*) as c
    FROM memory_events
    WHERE project = ?
    GROUP BY tier
    ORDER BY MIN(importance) DESC
  `).all(project);
    const lines = [];
    lines.push('  ── Importance Distribution ──');
    lines.push('');
    const maxCount = Math.max(...rows.map(r => r.c), 1);
    const barWidth = 25;
    for (const row of rows) {
        const barLen = Math.round((row.c / maxCount) * barWidth);
        const bar = '█'.repeat(barLen);
        lines.push(`  ${row.tier.padEnd(20)} ${bar} ${row.c}`);
    }
    return lines.join('\n');
}
function renderTimelineChart(project, days = 7) {
    const db = (0, database_1.getDb)();
    const lines = [];
    lines.push(`  ── Hourly Activity (last ${days} days) ──`);
    lines.push('');
    const since = new Date(Date.now() - days * 86400000).toISOString();
    const rows = db.prepare(`
    SELECT CAST(strftime('%H', created_at) AS INTEGER) as hour, COUNT(*) as c
    FROM memory_events
    WHERE project = ? AND created_at >= ?
    GROUP BY hour ORDER BY hour
  `).all(project, since);
    const hourMap = new Map(rows.map(r => [r.hour, r.c]));
    const maxCount = Math.max(...rows.map(r => r.c), 1);
    const barWidth = 25;
    for (let h = 0; h < 24; h++) {
        const count = hourMap.get(h) || 0;
        const barLen = Math.round((count / maxCount) * barWidth);
        const bar = '█'.repeat(barLen);
        const label = h.toString().padStart(2, '0') + ':00';
        lines.push(`  ${label} ${bar} ${count}`);
    }
    return lines.join('\n');
}
function renderFullDashboard(project) {
    const sections = [
        renderActivityChart(project, 14),
        '',
        renderTypeDistribution(project),
        '',
        renderImportanceChart(project),
    ];
    return sections.join('\n');
}
//# sourceMappingURL=memory-charts.js.map