import type { FastifyInstance } from 'fastify';
import type { Authenticate } from '../../../shared/auth.js';
import { requireRole } from '../../../shared/auth.js';
import type { InMemoryMeter } from '../infrastructure/no-op-telemetry.js';

export interface MetricsRoutesDeps {
  meter: InMemoryMeter;
  authenticate: Authenticate;
}

export async function registerMetricsRoutes(
  app: FastifyInstance,
  deps: MetricsRoutesDeps,
): Promise<void> {
  app.get('/ops/metrics', async (request, reply) => {
    const user = await deps.authenticate(request);
    requireRole(user, 'admin');
    return reply.send({ metrics: deps.meter.snapshot() });
  });
}
