/**
 * Controller du jeu
 */
import { asyncHandler } from '../middleware/errorHandler.js';
import gameService from '../services/game.service.js';

/**
 * Jouer avec un code ticket
 * POST /api/game/play
 */
export const play = asyncHandler(async (req, res) => {
  const { ticketCode } = req.body;
  const userId = req.user.id;

  const result = await gameService.playGame(userId, ticketCode);

  res.json({
    success: true,
    message: '🎉 Félicitations! Vous avez gagné!',
    data: result,
  });
});

/**
 * Récupérer les tickets de l'utilisateur connecté
 * GET /api/game/my-tickets
 */
export const getMyTickets = asyncHandler(async (req, res) => {
  const userId = req.user.id;
  const { page = 1, limit = 10, status } = req.query;

  const result = await gameService.getUserTickets(userId, page, limit, status);

  res.json({
    success: true,
    data: result,
  });
});

/**
 * Récupérer un ticket spécifique
 * GET /api/game/my-tickets/:id
 */
export const getMyTicket = asyncHandler(async (req, res) => {
  const userId = req.user.id;
  const { id } = req.params;

  const ticket = await gameService.getUserTicket(userId, id);

  res.json({
    success: true,
    data: ticket,
  });
});

/**
 * Récupérer un ticket spécifique par code
 * GET /api/game/ticket/:code
 */
export const getTicketByCode = asyncHandler(async (req, res) => {
  const userId = req.user.id;
  const { code } = req.params;

  const ticket = await gameService.getUserTicketByCode(userId, code);

  res.json({
    success: true,
    data: ticket,
  });
});

export default {
  play,
  getMyTickets,
  getMyTicket,
  getTicketByCode,
};
