const express = require('express');

const GameplayService = require('../services/gameplay');
const wrapAsync = require('../helpers').wrapAsync;

const router = express.Router();

router.get(
  '/active',
  wrapAsync(async (req, res) => {
    const payload = await GameplayService.getActiveSession(req.currentUser);
    res.status(200).send(payload);
  }),
);

router.get(
  '/sessions',
  wrapAsync(async (req, res) => {
    const payload = await GameplayService.listSessions(req.currentUser);
    res.status(200).send(payload);
  }),
);

router.get(
  '/sessions/:id',
  wrapAsync(async (req, res) => {
    const payload = await GameplayService.getSessionDetail(
      req.params.id,
      req.currentUser,
    );
    res.status(200).send(payload);
  }),
);

router.post(
  '/sessions/start',
  wrapAsync(async (req, res) => {
    const payload = await GameplayService.startSession(req.body, req.currentUser);
    res.status(200).send(payload);
  }),
);

router.post(
  '/sessions/:sessionId/rounds/:roundId/answer',
  wrapAsync(async (req, res) => {
    const payload = await GameplayService.answerRound(
      req.params.sessionId,
      req.params.roundId,
      req.body,
      req.currentUser,
    );
    res.status(200).send(payload);
  }),
);

router.use('/', require('../helpers').commonErrorHandler);

module.exports = router;
