import { getQueueStats, getJobsStatus } from '../../../../../frameworks/queue/pQueueAdapter.js';
function buildSupervisorBlock(store) {
    if (!store) {
        return { state: 'unknown', reason: null, lastCheckedAt: null };
    }
    const current = store.read();
    const lastCheckedAt = current.lastCheckedAt.getTime() === 0 ? null : current.lastCheckedAt.toISOString();
    return {
        state: current.state,
        reason: current.reason,
        lastCheckedAt,
    };
}
export const healthRoutes = async (fastify, opts) => {
    const { getConfig } = opts;
    fastify.get('/health', async () => {
        const supervisor = buildSupervisorBlock(opts.supervisorStatusStore);
        const status = supervisor.state === 'down' ? 'degraded' : 'ok';
        return {
            status,
            timestamp: new Date().toISOString(),
            supervisor,
        };
    });
    fastify.get('/api/status', async () => {
        const queueStats = getQueueStats();
        const jobs = getJobsStatus();
        const config = getConfig();
        const versionCheck = opts.versionCache?.get() ?? null;
        return {
            status: 'running',
            version: config.version,
            queue: queueStats,
            jobs,
            timestamp: new Date().toISOString(),
            latestVersion: versionCheck?.latestVersion ?? null,
            updateAvailable: versionCheck?.updateAvailable ?? false,
            versionCheckedAt: versionCheck?.checkedAt ?? null,
        };
    });
};
//# sourceMappingURL=health.routes.js.map