import { FastifyInstance } from 'fastify';
import { authenticate } from '../hooks/authenticate';
import * as clientService from '../services/client.service';

export default async function clientRoutes(fastify: FastifyInstance) {
  fastify.get('/', async (request) => {
    const { page = '1', limit = '20' } = request.query as any;
    const [data, total] = await clientService.listClients(parseInt(page), parseInt(limit));
    return { data, total, page: parseInt(page) };
  });

  fastify.post('/', { preHandler: authenticate }, async (request, reply) => {
    try {
      return await clientService.createClient(request.body);
    } catch (err: any) {
      return reply.status(err.statusCode || 500).send({ error: err.message || 'Error al crear el cliente.' });
    }
  });
}
