"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const auth_1 = require("../modules/auth");
const api = __importStar(require("../api/controlplane"));
const router = (0, express_1.Router)();
/**
 * GET /api/controlplane/apps
 * List available HotMesh application IDs.
 * Admin-only.
 */
router.get('/apps', auth_1.requireAdmin, async (_req, res) => {
    const result = await api.listApps();
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * GET /api/controlplane/rollcall?app_id=durable&delay=1000
 * Execute a roll call — discovers all engines and workers.
 * Admin-only.
 */
router.get('/rollcall', auth_1.requireAdmin, async (req, res) => {
    const result = await api.rollCall({
        appId: req.query.app_id || 'durable',
        delay: req.query.delay ? parseInt(req.query.delay, 10) : undefined,
    });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * POST /api/controlplane/throttle
 * Apply a throttle command to the mesh.
 * Admin-only.
 *
 * Body: { appId: string, throttle: number, topic?: string, guid?: string }
 *   throttle: ms delay (-1 = pause, 0 = resume, >0 = delay per msg)
 */
router.post('/throttle', auth_1.requireAdmin, async (req, res) => {
    const { appId = 'durable', throttle, topic, guid, scope } = req.body;
    const result = await api.applyThrottle({ appId, throttle, topic, guid, scope });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * GET /api/controlplane/streams?app_id=durable&duration=1h&stream=hmsh:durable:x:
 * Stream processing statistics — pending count + processed volume by time range.
 * Admin-only.
 *
 * duration: 15m | 30m | 1h | 1d | 7d (default: 1h)
 * stream: optional stream_name filter (specific task queue topic)
 */
router.get('/streams', auth_1.requireAdmin, async (req, res) => {
    const result = await api.getStreamStats({
        app_id: req.query.app_id || 'durable',
        duration: req.query.duration || '1h',
        stream: req.query.stream || undefined,
    });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * GET /api/controlplane/stream-messages?namespace=durable&source=worker
 *     &limit=25&offset=0&sort_by=created_at&order=desc
 *     &status=pending&stream_name=hmsh:durable:w:&msg_type=WORKER
 *
 * Browse stream messages with pagination, filtering, and sorting.
 * Both namespace and source are required — engine and worker streams
 * are separate tables with different schemas.
 * Admin-only.
 */
router.get('/stream-messages', auth_1.requireAdmin, async (req, res) => {
    const result = await api.listStreamMessages({
        namespace: req.query.namespace,
        source: req.query.source,
        limit: req.query.limit ? parseInt(req.query.limit, 10) : undefined,
        offset: req.query.offset ? parseInt(req.query.offset, 10) : undefined,
        sort_by: req.query.sort_by || undefined,
        order: req.query.order || undefined,
        status: req.query.status || undefined,
        stream_name: req.query.stream_name || undefined,
        msg_type: req.query.msg_type || undefined,
        topic: req.query.topic || undefined,
        workflow_name: req.query.workflow_name || undefined,
        jid: req.query.jid || undefined,
        aid: req.query.aid || undefined,
    });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * POST /api/controlplane/subscribe
 * Start the quorum→NATS bridge for an appId.
 * Subscribes to the HotMesh quorum channel and republishes
 * all control plane messages to NATS on `lt.mesh.*` topics.
 * Admin-only.
 *
 * Body: { appId: string }
 */
router.post('/subscribe', auth_1.requireAdmin, async (req, res) => {
    const { appId = 'durable' } = req.body;
    const result = await api.subscribeMesh({ appId });
    res.status(result.status).json(result.data ?? { error: result.error });
});
exports.default = router;
