"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const remote_metrics_db_1 = require("../utils/remote-metrics-db");
const router = express_1.default.Router();
// @group Constants : Default and maximum query bounds
const DEFAULT_RANGE_MS = 3600000; // 1 hour
const MAX_RANGE_MS = 7 * 24 * 3600000; // 7 days
const MAX_POINTS = 500;
// @group Utilities : Parse and clamp a query-string integer
const parseIntParam = (raw, fallback, min, max) => {
    const n = parseInt(raw, 10);
    if (!Number.isFinite(n))
        return fallback;
    return Math.max(min, Math.min(max, n));
};
/**
 * List all remote connections that have recorded metrics
 * GET /api/remote-metrics/connections
 */
router.get('/connections', (_req, res) => {
    try {
        const connections = remote_metrics_db_1.remoteMetricsDB.getConnectionsWithData();
        res.json({ success: true, connections });
    }
    catch (err) {
        res.status(500).json({ success: false, error: err.message });
    }
});
/**
 * List all process names recorded for a connection
 * GET /api/remote-metrics/:connectionId/processes
 */
router.get('/:connectionId/processes', (req, res) => {
    try {
        const { connectionId } = req.params;
        const names = remote_metrics_db_1.remoteMetricsDB.getProcessNames(connectionId);
        res.json({ success: true, processes: names });
    }
    catch (err) {
        res.status(500).json({ success: false, error: err.message });
    }
});
/**
 * Latest snapshot (most recent metric point per process) for a connection
 * GET /api/remote-metrics/:connectionId/snapshot
 */
router.get('/:connectionId/snapshot', (req, res) => {
    try {
        const { connectionId } = req.params;
        const snapshot = remote_metrics_db_1.remoteMetricsDB.getLatestSnapshot(connectionId);
        res.json({ success: true, snapshot });
    }
    catch (err) {
        res.status(500).json({ success: false, error: err.message });
    }
});
/**
 * Time-series metrics for one process on a connection
 * GET /api/remote-metrics/:connectionId/:processName
 *   ?from=<unix-ms>  (default: now - 1h)
 *   ?to=<unix-ms>    (default: now)
 *   ?maxPoints=<n>   (default: 500, max: 1000)
 */
router.get('/:connectionId/:processName', (req, res) => {
    try {
        const { connectionId, processName } = req.params;
        const now = Date.now();
        const to = parseIntParam(req.query.to, now, 0, now + 60000);
        const from = parseIntParam(req.query.from, to - DEFAULT_RANGE_MS, 0, now);
        // Clamp range to max window
        const clampedFrom = Math.max(from, to - MAX_RANGE_MS);
        const maxPoints = parseIntParam(req.query.maxPoints, MAX_POINTS, 50, 1000);
        const metrics = remote_metrics_db_1.remoteMetricsDB.getMetrics(connectionId, processName, clampedFrom, to, maxPoints);
        res.json({ success: true, metrics });
    }
    catch (err) {
        res.status(500).json({ success: false, error: err.message });
    }
});
exports.default = router;
