"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const auth_middleware_1 = require("../middleware/auth-middleware");
const log_types_1 = require("../../services/logging/log-types");
const logs_dashboard_service_1 = require("../services/logs-dashboard-service");
const router = (0, express_1.Router)();
const LOGS_LOCAL_ACCESS_ERROR = 'Logs endpoints require localhost access when dashboard auth is disabled.';
router.use((req, res, next) => {
    if ((0, auth_middleware_1.requireLocalAccessWhenAuthDisabled)(req, res, LOGS_LOCAL_ACCESS_ERROR)) {
        next();
    }
});
router.get('/config', (_req, res) => {
    res.json({ logging: (0, logs_dashboard_service_1.getDashboardLoggingConfig)() });
});
router.put('/config', (req, res) => {
    const updates = req.body;
    if (!updates || typeof updates !== 'object' || Array.isArray(updates)) {
        res.status(400).json({ error: 'Invalid request body. Must be an object.' });
        return;
    }
    const { enabled, level, rotate_mb: rotateMb, retain_days: retainDays, redact, live_buffer_size: liveBufferSize, } = updates;
    if (enabled !== undefined && typeof enabled !== 'boolean') {
        res.status(400).json({ error: 'enabled must be a boolean' });
        return;
    }
    if (level !== undefined && !(0, log_types_1.isLoggingLevel)(String(level))) {
        res.status(400).json({ error: 'level must be one of error, warn, info, debug' });
        return;
    }
    const numericPairs = [
        ['rotate_mb', rotateMb],
        ['retain_days', retainDays],
        ['live_buffer_size', liveBufferSize],
    ];
    for (const [field, value] of numericPairs) {
        if (value !== undefined && (!Number.isInteger(value) || Number(value) < 1)) {
            res.status(400).json({ error: `${field} must be a positive integer` });
            return;
        }
    }
    if (redact !== undefined && typeof redact !== 'boolean') {
        res.status(400).json({ error: 'redact must be a boolean' });
        return;
    }
    res.json({
        success: true,
        logging: (0, logs_dashboard_service_1.updateDashboardLoggingConfig)({
            enabled: enabled,
            level: level,
            rotate_mb: rotateMb,
            retain_days: retainDays,
            redact: redact,
            live_buffer_size: liveBufferSize,
        }),
    });
});
router.get('/sources', (_req, res) => {
    res.json({ sources: (0, logs_dashboard_service_1.listDashboardLogSources)() });
});
router.get('/entries', (req, res) => {
    const { source, level, search, limit } = req.query;
    const parsedLimit = typeof limit === 'string' && Number.isInteger(Number(limit)) ? Number(limit) : undefined;
    res.json({
        entries: (0, logs_dashboard_service_1.listDashboardLogEntries)({
            source: typeof source === 'string' ? source : undefined,
            level: typeof level === 'string' && (0, log_types_1.isLoggingLevel)(level) ? level : undefined,
            search: typeof search === 'string' ? search : undefined,
            limit: parsedLimit,
        }),
    });
});
exports.default = router;
//# sourceMappingURL=logs-routes.js.map