import { DASHBOARD_API_VERSION } from '../contract.js';
import { INSTALL_ARCH_GENERATION, loadManifest } from '../../install/manifest.js';
import { getToolHealthSummary } from '../../agents/adapters/mcp/tool-health.js';
export const handleHealthRoutes = async (ctx, req, res, url) => {
    if (req.method === 'GET'
        && (url.pathname === '/api/version' || url.pathname === '/api/dashboard/version')) {
        // Stable version endpoint so external probes (CI smoke, foreign installs
        // sharing port 3377) can identify the running dashboard generation.
        const manifest = (() => { try {
            return loadManifest();
        }
        catch {
            return null;
        } })();
        ctx.respondJson(res, {
            dashboardApiVersion: DASHBOARD_API_VERSION,
            architectureGeneration: INSTALL_ARCH_GENERATION,
            installArchGeneration: manifest?.architectureGeneration ?? null,
            product: 'nexus-prime',
        });
        return true;
    }
    if (req.method === 'GET' && url.pathname === '/api/license') {
        const { getSharedLicenseManager } = await import('../../licensing/index.js');
        ctx.respondJson(res, getSharedLicenseManager().getStatus());
        return true;
    }
    if (req.method === 'GET' && url.pathname === '/api/runtimes') {
        ctx.runtimeRegistry.pruneStalePublic();
        ctx.respondJson(res, ctx.runtimeRegistry.list());
        return true;
    }
    if (req.method === 'POST' && url.pathname === '/api/runtime/prune') {
        ctx.runtimeRegistry.pruneStalePublic();
        ctx.respondJson(res, { pruned: true, remaining: ctx.runtimeRegistry.list().length });
        return true;
    }
    if (req.method === 'GET' && url.pathname === '/api/health') {
        if (url.searchParams.get('probe') === '1') {
            ctx.respondJson(res, ctx.collectCompatibilityHealth());
            return true;
        }
        await ctx.respondCachedJson(res, 'health', 15_000, () => ctx.collectHealth());
        return true;
    }
    if (req.method === 'GET' && url.pathname === '/api/runtime/tool-health') {
        ctx.respondJson(res, getToolHealthSummary());
        return true;
    }
    return false;
};
