"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.servicesRouter = void 0;
const express_1 = require("express");
const auth_middleware_1 = require("../../middleware/auth.middleware");
const rbac_middleware_1 = require("../../middleware/rbac.middleware");
const env_1 = require("../../config/env");
const logger_1 = require("../../utils/logger");
const metrics_1 = require("../../utils/metrics");
const health_check_1 = require("../../utils/health-check");
const router = (0, express_1.Router)();
router.use(auth_middleware_1.authenticate);
router.use((0, rbac_middleware_1.requireRole)('SUPER_ADMIN'));
// Removed duplicated isServiceOnline - now using shared utility from utils/health-check.ts
// GET /api/services/status — check NocoDB, n8n, Gitea availability
router.get('/status', async (_req, res, next) => {
    try {
        const [nocodbOnline, n8nOnline, giteaOnline, mailhogOnline, miniqrOnline, excalidrawOnline, homepageOnline, vaultwardenOnline, rocketchatOnline, gancioOnline, jitsiOnline] = await Promise.all([
            (0, health_check_1.isServiceOnline)(env_1.env.NOCODB_URL),
            (0, health_check_1.isServiceOnline)(env_1.env.N8N_URL),
            (0, health_check_1.isServiceOnline)(env_1.env.GITEA_URL),
            (0, health_check_1.isServiceOnline)(env_1.env.MAILHOG_URL),
            (0, health_check_1.isServiceOnline)(env_1.env.MINI_QR_URL),
            (0, health_check_1.isServiceOnline)(env_1.env.EXCALIDRAW_URL),
            (0, health_check_1.isServiceOnline)(env_1.env.HOMEPAGE_URL),
            (0, health_check_1.isServiceOnline)(env_1.env.VAULTWARDEN_URL),
            (0, health_check_1.isServiceOnline)(`${env_1.env.ROCKETCHAT_URL}/api/info`),
            (0, health_check_1.isServiceOnline)(env_1.env.GANCIO_URL),
            (0, health_check_1.isServiceOnline)(env_1.env.JITSI_URL),
        ]);
        // Update Prometheus gauges
        (0, metrics_1.setServiceUp)('nocodb', nocodbOnline);
        (0, metrics_1.setServiceUp)('n8n', n8nOnline);
        (0, metrics_1.setServiceUp)('gitea', giteaOnline);
        (0, metrics_1.setServiceUp)('mailhog', mailhogOnline);
        (0, metrics_1.setServiceUp)('miniqr', miniqrOnline);
        (0, metrics_1.setServiceUp)('excalidraw', excalidrawOnline);
        (0, metrics_1.setServiceUp)('homepage', homepageOnline);
        (0, metrics_1.setServiceUp)('vaultwarden', vaultwardenOnline);
        (0, metrics_1.setServiceUp)('rocketchat', rocketchatOnline);
        (0, metrics_1.setServiceUp)('gancio', gancioOnline);
        (0, metrics_1.setServiceUp)('jitsi', jitsiOnline);
        res.json({
            nocodb: { online: nocodbOnline, url: env_1.env.NOCODB_URL },
            n8n: { online: n8nOnline, url: env_1.env.N8N_URL },
            gitea: { online: giteaOnline, url: env_1.env.GITEA_URL },
            mailhog: { online: mailhogOnline, url: env_1.env.MAILHOG_URL },
            miniqr: { online: miniqrOnline, url: env_1.env.MINI_QR_URL },
            excalidraw: { online: excalidrawOnline, url: env_1.env.EXCALIDRAW_URL },
            homepage: { online: homepageOnline, url: env_1.env.HOMEPAGE_URL },
            vaultwarden: { online: vaultwardenOnline, url: env_1.env.VAULTWARDEN_URL },
            rocketchat: { online: rocketchatOnline, url: env_1.env.ROCKETCHAT_URL },
            gancio: { online: gancioOnline, url: env_1.env.GANCIO_URL },
            jitsi: { online: jitsiOnline, url: env_1.env.JITSI_URL },
        });
    }
    catch (err) {
        logger_1.logger.error('Failed to check services status', err);
        next(err);
    }
});
// GET /api/services/config — return public-facing port numbers + subdomain info for iframe URLs
router.get('/config', async (_req, res, _next) => {
    res.json({
        nocodbPort: env_1.env.NOCODB_EMBED_PORT,
        n8nPort: env_1.env.N8N_EMBED_PORT,
        giteaPort: env_1.env.GITEA_EMBED_PORT,
        mailhogPort: env_1.env.MAILHOG_EMBED_PORT,
        miniqrPort: env_1.env.MINI_QR_EMBED_PORT,
        excalidrawPort: env_1.env.EXCALIDRAW_EMBED_PORT,
        domain: env_1.env.DOMAIN,
        nocodbSubdomain: 'db',
        n8nSubdomain: 'n8n',
        giteaSubdomain: 'git',
        mailhogSubdomain: 'mail',
        miniqrSubdomain: 'qr',
        excalidrawSubdomain: 'draw',
        // Listmonk (newsletter platform)
        listmonkPort: 9001,
        listmonkSubdomain: 'listmonk',
        // Code Server (web IDE)
        codeServerPort: env_1.env.CODE_SERVER_PORT,
        codeServerSubdomain: 'code',
        // MkDocs (documentation)
        mkdocsPort: env_1.env.MKDOCS_PORT,
        mkdocsSubdomain: 'docs',
        // Grafana (metrics visualization)
        grafanaPort: env_1.env.GRAFANA_EMBED_PORT,
        grafanaSubdomain: 'grafana',
        // Alertmanager (alert routing)
        alertmanagerPort: env_1.env.ALERTMANAGER_EMBED_PORT,
        alertmanagerSubdomain: 'alertmanager',
        // Homepage (service dashboard)
        homepagePort: env_1.env.HOMEPAGE_EMBED_PORT,
        homepageSubdomain: 'home',
        // Vaultwarden (password manager)
        vaultwardenPort: env_1.env.VAULTWARDEN_EMBED_PORT,
        vaultwardenSubdomain: 'vault',
        // Rocket.Chat (team chat)
        rocketchatPort: env_1.env.ROCKETCHAT_EMBED_PORT,
        rocketchatSubdomain: 'chat',
        // Gancio (event management)
        gancioPort: env_1.env.GANCIO_EMBED_PORT,
        gancioSubdomain: 'events',
        // Jitsi Meet (video conferencing)
        jitsiPort: env_1.env.JITSI_EMBED_PORT,
        jitsiSubdomain: 'meet',
    });
});
exports.servicesRouter = router;
//# sourceMappingURL=services.routes.js.map