"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerNginxLifecycleRoutes = registerNginxLifecycleRoutes;
const nginx_binding_store_1 = require("../../plugins/nginx.binding.store");
const nginx_route_context_1 = require("./nginx-route.context");
function registerNginxLifecycleRoutes(context) {
    const { fastify, nginx } = context;
    fastify.get('/status', async (_request, reply) => {
        const instance = nginx.service.getInstance();
        const status = await nginx.service.getStatus();
        return reply.send({
            instance,
            status,
        });
    });
    fastify.get('/stats', async (_request, reply) => {
        try {
            const instance = nginx.service.getInstance();
            const status = await nginx.service.getStatus();
            const servers = await nginx.server.getAllServers();
            const enabled = servers.filter(item => item.enabled).length;
            return reply.send({
                success: true,
                instance,
                status,
                serverSummary: {
                    total: servers.length,
                    enabled,
                    disabled: Math.max(servers.length - enabled, 0),
                },
            });
        }
        catch (error) {
            return (0, nginx_route_context_1.sendBadRequest)(reply, error);
        }
    });
    fastify.post('/bind', async (request, reply) => {
        const { path } = request.body;
        try {
            const instance = await nginx.service.bind(path);
            try {
                await (0, nginx_binding_store_1.savePersistedNginxPath)(instance.path);
            }
            catch (persistError) {
                fastify.log.warn(`[nginx] binding persisted failed: ${String(persistError)}`);
            }
            return reply.send({
                success: true,
                instance,
            });
        }
        catch (error) {
            return (0, nginx_route_context_1.sendBadRequest)(reply, error);
        }
    });
    fastify.post('/unbind', async (_request, reply) => {
        nginx.service.unbind();
        try {
            await (0, nginx_binding_store_1.clearPersistedNginxPath)();
        }
        catch (error) {
            fastify.log.warn(`[nginx] clear persisted binding failed: ${String(error)}`);
        }
        return reply.send({
            success: true,
        });
    });
    fastify.get('/local-ip', async (_request, reply) => {
        const result = await nginx.service.getLocalIp();
        return reply.send(result);
    });
    fastify.post('/start', async (_request, reply) => {
        const result = await nginx.service.start();
        if (result.success) {
            fastify.core.sysLog.info({ scope: 'system', source: 'server', text: '[Nginx] 启动成功' });
        }
        else {
            fastify.core.sysLog.error({ scope: 'system', source: 'server', text: `[Nginx] 启动失败: ${result.error || '未知错误'}` });
        }
        return reply.send(result);
    });
    fastify.post('/stop', async (_request, reply) => {
        const result = await nginx.service.stop();
        if (result.success) {
            fastify.core.sysLog.warn({ scope: 'system', source: 'server', text: '[Nginx] 已停止' });
        }
        else {
            fastify.core.sysLog.error({ scope: 'system', source: 'server', text: `[Nginx] 停止失败: ${result.error || '未知错误'}` });
        }
        return reply.send(result);
    });
    fastify.post('/reload', async (_request, reply) => {
        const result = await nginx.service.reload();
        if (result.success) {
            fastify.core.sysLog.info({ scope: 'system', source: 'server', text: '[Nginx] 配置重载成功' });
        }
        else {
            fastify.core.sysLog.error({ scope: 'system', source: 'server', text: `[Nginx] 重载失败: ${result.error || '未知错误'}` });
        }
        return reply.send(result);
    });
    fastify.post('/test', async (_request, reply) => {
        const result = await nginx.service.testConfig();
        if (result.valid) {
            fastify.core.sysLog.success({ scope: 'system', source: 'server', text: '[Nginx] 配置检测通过' });
        }
        else {
            fastify.core.sysLog.error({ scope: 'system', source: 'server', text: `[Nginx] 配置检测失败: ${(result.errors || []).join(', ')}` });
        }
        return reply.send(result);
    });
}
