import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
import { GamesController } from '../controllers/gamesController';
import { createGameSchema, updateGameSchema } from '../validators/gameValidator';
import { ZodError } from 'zod';

async function gamesRoutes(app: FastifyInstance) {
  /**
   * GET /api/games
   * Listar todos los juegos
   */
  app.get<{ Reply: any[] }>('/games', async (request, reply) => {
    const games = await GamesController.listGames();
    return reply.code(200).send(games);
  });

  /**
   * POST /api/games
   * Crear un nuevo juego
   */
  app.post<{ Body: any; Reply: any }>('/games', async (request, reply) => {
    try {
      // Validar entrada con Zod
      const validated = createGameSchema.parse(request.body);
      const game = await GamesController.createGame(validated);
      return reply.code(201).send(game);
    } catch (error) {
      if (error instanceof ZodError) {
        return reply.code(400).send({
          error: 'Validación fallida',
          details: error.errors,
        });
      }
      throw error;
    }
  });

  /**
   * PUT /api/games/:id
   * Actualizar un juego existente
   */
  app.put<{ Params: { id: string }; Body: any; Reply: any }>(
    '/games/:id',
    async (request, reply) => {
      try {
        // Validar entrada con Zod
        const validated = updateGameSchema.parse(request.body);
        const game = await GamesController.updateGame(request.params.id, validated);
        return reply.code(200).send(game);
      } catch (error) {
        if (error instanceof ZodError) {
          return reply.code(400).send({
            error: 'Validación fallida',
            details: error.errors,
          });
        }
        if (error instanceof Error && error.message.includes('not found')) {
          return reply.code(404).send({
            error: 'Juego no encontrado',
          });
        }
        throw error;
      }
    }
  );

  /**
   * DELETE /api/games/:id
   * Eliminar un juego
   */
  app.delete<{ Params: { id: string }; Reply: any }>('/games/:id', async (request, reply) => {
    try {
      await GamesController.deleteGame(request.params.id);
      return reply.code(204).send();
    } catch (error) {
      if (error instanceof Error && error.message.includes('no encontrado')) {
        return reply.code(404).send({
          error: 'Juego no encontrado',
        });
      }
      throw error;
    }
  });
}

export default gamesRoutes;

/**
 * Metadatos:
 * Autor: GitHub Copilot
 * Última actualización: 2026-02-11
 */
