"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const database_1 = require("../../../06-memory/database/database");
const router = (0, express_1.Router)();
// Memory stats
router.get('/memory/stats', (_req, res) => {
    const db = (0, database_1.getDb)();
    const tables = db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'").all();
    const stats = {};
    for (const { name } of tables) {
        try {
            const row = db.prepare(`SELECT COUNT(*) as count FROM "${name}"`).get();
            stats[name] = row.count;
        }
        catch {
            stats[name] = 0;
        }
    }
    res.json({ tables: stats, totalTables: tables.length });
});
// Memory outcomes
router.get('/memory/outcomes', (req, res) => {
    const db = (0, database_1.getDb)();
    const limit = Number(req.query.limit) || 100;
    const rows = db.prepare('SELECT * FROM outcomes ORDER BY created_at DESC LIMIT ?').all(limit);
    res.json({ outcomes: rows });
});
// Memory failures
router.get('/memory/failures', (_req, res) => {
    const db = (0, database_1.getDb)();
    try {
        const rows = db.prepare('SELECT * FROM failure_memory ORDER BY failure_rate DESC LIMIT 50').all();
        res.json({ failures: rows });
    }
    catch {
        res.json({ failures: [] });
    }
});
// Memory workflows
router.get('/memory/workflows', (_req, res) => {
    const db = (0, database_1.getDb)();
    try {
        const rows = db.prepare('SELECT * FROM workflow_sessions ORDER BY started_at DESC LIMIT 20').all();
        res.json({ workflows: rows });
    }
    catch {
        res.json({ workflows: [] });
    }
});
// Memory time-saved
router.get('/memory/time-saved', (_req, res) => {
    const db = (0, database_1.getDb)();
    try {
        const rows = db.prepare('SELECT * FROM time_saved ORDER BY created_at DESC LIMIT 50').all();
        res.json({ records: rows });
    }
    catch {
        res.json({ records: [] });
    }
});
// Clear memory
router.delete('/memory/clear', (_req, res) => {
    const db = (0, database_1.getDb)();
    const tables = ['memory_events', 'memory_patterns', 'memory_summaries', 'handoff_entries'];
    for (const table of tables) {
        try {
            db.prepare(`DELETE FROM ${table}`).run();
        }
        catch { /* ignore */ }
    }
    res.json({ cleared: true });
});
exports.default = router;
//# sourceMappingURL=memory.js.map