"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerNginxModuleRoutes = registerNginxModuleRoutes;
const nginx_route_context_1 = require("./nginx-route.context");
function registerNginxModuleRoutes(context) {
    const { fastify, nginx } = context;
    fastify.get('/upstreams', async (_request, reply) => {
        try {
            const upstreams = await nginx.module.getUpstreams();
            return reply.send({
                success: true,
                upstreams,
            });
        }
        catch (error) {
            return (0, nginx_route_context_1.sendBadRequest)(reply, error);
        }
    });
    fastify.put('/upstreams', async (request, reply) => {
        try {
            await nginx.module.saveUpstreams(request.body?.upstreams || []);
            return reply.send({
                success: true,
            });
        }
        catch (error) {
            return (0, nginx_route_context_1.sendBadRequest)(reply, error);
        }
    });
    fastify.get('/ssl/certificates', async (_request, reply) => {
        try {
            const certificates = await nginx.module.getSslCertificates();
            return reply.send({
                success: true,
                certificates,
            });
        }
        catch (error) {
            return (0, nginx_route_context_1.sendBadRequest)(reply, error);
        }
    });
    fastify.put('/ssl/certificates', async (request, reply) => {
        try {
            await nginx.module.saveSslCertificates(request.body?.certificates || []);
            return reply.send({
                success: true,
            });
        }
        catch (error) {
            return (0, nginx_route_context_1.sendBadRequest)(reply, error);
        }
    });
    fastify.get('/traffic', async (_request, reply) => {
        try {
            const traffic = await nginx.module.getTrafficConfig();
            return reply.send({
                success: true,
                traffic,
            });
        }
        catch (error) {
            return (0, nginx_route_context_1.sendBadRequest)(reply, error);
        }
    });
    fastify.put('/traffic', async (request, reply) => {
        try {
            await nginx.module.saveTrafficConfig(request.body);
            return reply.send({
                success: true,
            });
        }
        catch (error) {
            return (0, nginx_route_context_1.sendBadRequest)(reply, error);
        }
    });
    fastify.get('/performance', async (_request, reply) => {
        try {
            const performance = await nginx.module.getPerformanceConfig();
            return reply.send({
                success: true,
                performance,
            });
        }
        catch (error) {
            return (0, nginx_route_context_1.sendBadRequest)(reply, error);
        }
    });
    fastify.put('/performance', async (request, reply) => {
        try {
            await nginx.module.savePerformanceConfig(request.body);
            return reply.send({
                success: true,
            });
        }
        catch (error) {
            return (0, nginx_route_context_1.sendBadRequest)(reply, error);
        }
    });
    fastify.get('/module/settings', async (_request, reply) => {
        try {
            const settings = await nginx.module.getModuleSettings();
            return reply.send({
                success: true,
                settings,
            });
        }
        catch (error) {
            return (0, nginx_route_context_1.sendBadRequest)(reply, error);
        }
    });
    fastify.put('/module/settings', async (request, reply) => {
        try {
            const settings = await nginx.module.saveModuleSettings(request.body || {});
            return reply.send({
                success: true,
                settings,
            });
        }
        catch (error) {
            return (0, nginx_route_context_1.sendBadRequest)(reply, error);
        }
    });
}
