import type { FastifyInstance } from 'fastify';
import { APP_VERSION } from '@mcpctl/shared';

export interface HealthDeps {
  checkDb: () => Promise<boolean>;
}

export function registerHealthRoutes(app: FastifyInstance, deps: HealthDeps): void {
  app.get('/health', async (_request, reply) => {
    const dbOk = await deps.checkDb().catch(() => false);

    const status = dbOk ? 'healthy' : 'degraded';
    const statusCode = dbOk ? 200 : 503;

    return reply.code(statusCode).send({
      status,
      version: APP_VERSION,
      uptime: process.uptime(),
      timestamp: new Date().toISOString(),
      checks: {
        database: dbOk ? 'ok' : 'error',
      },
    });
  });

  // Simple liveness probe
  app.get('/healthz', async (_request, reply) => {
    return reply.code(200).send({ status: 'ok' });
  });
}
