import type { FastifyInstance } from 'fastify';
import type { SecretService } from '../services/secret.service.js';

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

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

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

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

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