import type { FastifyInstance } from 'fastify';
import type { TemplateService } from '../services/template.service.js';

export function registerTemplateRoutes(
  app: FastifyInstance,
  service: TemplateService,
): void {
  app.get<{ Querystring: { name?: string } }>('/api/v1/templates', async (request) => {
    const namePattern = request.query.name;
    return service.list(namePattern);
  });

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

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

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

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