import type { FastifyInstance } from 'fastify';
import type { RbacDefinitionService } from '../services/rbac-definition.service.js';

export function registerRbacRoutes(
  app: FastifyInstance,
  service: RbacDefinitionService,
): void {
  app.get('/api/v1/rbac', async () => {
    return service.list();
  });

  app.get<{ Params: { id: string } }>('/api/v1/rbac/:id', async (request) => {
    return service.getById(request.params.id);
  });

  app.post('/api/v1/rbac', async (request, reply) => {
    const def = await service.create(request.body);
    reply.code(201);
    return def;
  });

  app.put<{ Params: { id: string } }>('/api/v1/rbac/:id', async (request) => {
    return service.update(request.params.id, request.body);
  });

  app.delete<{ Params: { id: string } }>('/api/v1/rbac/:id', async (request, reply) => {
    await service.delete(request.params.id);
    reply.code(204);
  });
}
