"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createServerRouter = createServerRouter;
const express_1 = __importDefault(require("express"));
const zod_1 = require("zod");
const serverManager_js_1 = require("../serverManager.js");
const mcpServerManager_js_1 = require("../mcpServerManager.js");
const getServerLogs_js_1 = require("../../use-cases/getServerLogs.js");
const getMCPServerLogs_js_1 = require("../../use-cases/getMCPServerLogs.js");
const restartServer_js_1 = require("../../use-cases/restartServer.js");
const restartMCPServer_js_1 = require("../../use-cases/restartMCPServer.js");
const clearServerLogs_js_1 = require("../../use-cases/clearServerLogs.js");
const clearMCPServerLogs_js_1 = require("../../use-cases/clearMCPServerLogs.js");
// Validation middleware
const validateQuery = (schema) => {
    return (req, res, next) => {
        try {
            const validatedQuery = schema.parse(req.query);
            req.validatedQuery = validatedQuery;
            next();
        }
        catch (error) {
            if (error instanceof zod_1.z.ZodError) {
                res.status(400).json({ error: 'Validation error', details: error.errors });
                return;
            }
            next(error);
        }
    };
};
// Error handling middleware
const asyncHandler = (fn) => {
    return (req, res, next) => {
        Promise.resolve(fn(req, res, next)).catch(next);
    };
};
function createServerRouter() {
    const router = express_1.default.Router();
    // Initialize managers and use cases
    const serverManager = serverManager_js_1.ServerManager.getInstance();
    const mcpServerManager = mcpServerManager_js_1.MCPServerManager.getInstance();
    const getServerLogs = new getServerLogs_js_1.GetServerLogs(serverManager);
    const getMCPServerLogs = new getMCPServerLogs_js_1.GetMCPServerLogs(mcpServerManager);
    const restartServer = new restartServer_js_1.RestartServer(serverManager);
    const restartMCPServer = new restartMCPServer_js_1.RestartMCPServer(mcpServerManager);
    const clearServerLogs = new clearServerLogs_js_1.ClearServerLogs(serverManager);
    const clearMCPServerLogs = new clearMCPServerLogs_js_1.ClearMCPServerLogs(mcpServerManager);
    // GET /api/server/logs
    router.get('/logs', validateQuery(zod_1.z.object({ type: zod_1.z.enum(['all', 'stdout', 'stderr']).optional() })), asyncHandler(async (req, res) => {
        const { type } = req.validatedQuery || req.query;
        const validType = ['all', 'stdout', 'stderr'].includes(type) ? type : 'all';
        const logs = await getServerLogs.execute(validType);
        res.json(logs);
    }));
    // GET /api/server/mcp-logs
    router.get('/mcp-logs', validateQuery(zod_1.z.object({ type: zod_1.z.enum(['all', 'stdout', 'stderr']).optional() })), asyncHandler(async (req, res) => {
        const { type } = req.validatedQuery || req.query;
        const validType = ['all', 'stdout', 'stderr'].includes(type) ? type : 'all';
        const logs = await getMCPServerLogs.execute(validType);
        res.json(logs);
    }));
    // POST /api/server/restart
    router.post('/restart', asyncHandler(async (req, res) => {
        await restartServer.execute();
        res.json({ success: true });
    }));
    // POST /api/server/mcp/restart
    router.post('/mcp/restart', asyncHandler(async (req, res) => {
        await restartMCPServer.execute();
        res.json({ success: true });
    }));
    // POST /api/server/logs/clear
    router.post('/logs/clear', validateQuery(zod_1.z.object({ type: zod_1.z.enum(['all', 'stdout', 'stderr']).optional() })), asyncHandler(async (req, res) => {
        const { type } = req.body || {};
        const validType = ['all', 'stdout', 'stderr'].includes(type) ? type : 'all';
        await clearServerLogs.execute(validType);
        res.json({ success: true });
    }));
    // POST /api/server/mcp-logs/clear
    router.post('/mcp-logs/clear', validateQuery(zod_1.z.object({ type: zod_1.z.enum(['all', 'stdout', 'stderr']).optional() })), asyncHandler(async (req, res) => {
        const { type } = req.body || {};
        const validType = ['all', 'stdout', 'stderr'].includes(type) ? type : 'all';
        await clearMCPServerLogs.execute(validType);
        res.json({ success: true });
    }));
    return router;
}
//# sourceMappingURL=serverRoutes.js.map