"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = systemRoutes;
const env_1 = require("../env");
function isLocalAddress(ip) {
    return (ip === '127.0.0.1' ||
        ip === '::1' ||
        ip === '::ffff:127.0.0.1');
}
async function systemRoutes(fastify) {
    fastify.get("/health", async () => ({
        ts: Date.now(),
        name: "ngm-server",
        pid: process.pid,
        uptime: process.uptime(),
        version: process.env.npm_package_version,
        dataDir: env_1.env.dataDir
    }));
    fastify.post("/shutdown", async (request, reply) => {
        if (!isLocalAddress(request.ip)) {
            fastify.log.warn(`Shutdown rejected: not localhost (${request.ip})`);
            return reply.code(403).send({ ok: false, error: 'Localhost only' });
        }
        const token = request.headers["x-ngm-shutdown-token"];
        const expected = env_1.env.shutdownToken;
        if (!expected) {
            fastify.log.warn("Shutdown rejected: no shutdown token configured");
            return reply.code(403).send({ ok: false, error: 'Shutdown token required' });
        }
        if (token !== expected) {
            fastify.log.warn("Shutdown rejected: invalid token");
            return reply.code(403).send({ ok: false, error: 'Invalid shutdown token' });
        }
        fastify.log.info("Shutdown requested via /shutdown");
        setTimeout(async () => {
            try {
                await fastify.close();
                process.exit(0);
            }
            catch (e) {
                console.error("Graceful shutdown failed", e);
                process.exit(1);
            }
        }, 50);
        return { ok: true };
    });
}
