const Game = require('../models').Game;

module.exports = {
  /**
   * Creates a new Game object on the server
   *
   * @route POST /games
   *
   * @param {*} req req.body.ATTRIBUTE
   * @param {*} res res.status
   */
  create(req, res) {
    return Game
      .create({
        rank: req.body.rank,
        map: req.body.map,
        outcome: req.body.outcome,
      })
      .then((game) => res.status(201).send({
        status: 201,
        message: 'Game created',
        data: game,
      }))
      .catch((error) => res.status(500).send({
        status: 500,
        message: 'Internal server error',
        errors: [error],
      }));
  },

  /**
   * Returns all entries from the server
   * Note: Should be changed to a findById in future multi-user versions
   *
   * @route GET /games
   *
   * @param {*} req
   * @param {*} res res.status
   */
  findAll(req, res) {
    return Game
      .findAll()
      .then(games => res.status(200).send({
        status: 200,
        message: 'Games found',
        data: games,
      }))
      .catch(error => res.status(500).send({
        status: 500,
        message: 'Internal server error',
        errors: [error],
      }));
  },

  /**
   * Updates the database with new data from a put request by gameId
   *
   * @route PUT /games/:gameId
   *
   * @param {*} req req.params.gameId from the URL
   * @param {*} res
   */
  update(req, res) {
    return Game
      .findById(req.params.gameId)
      .then(game => {
        if (!game) {
          throw {
            status: 404,
            message: 'Game not found',
            errors: ['Game not found'],
          };
        }

        return game
          .update({
            game_number: req.body.game_number || game.game_number,
            sr_change: req.body.sr_change || game.sr_change,
            rank: req.body.rank || game.rank,
            streak: req.body.streak || game.streak,
            map: req.body.map || game.map,
            score: req.body.score || game.score,
            group_size: req.body.group_size || game.group_size,
            heroes: req.body.heroes || game.heroes,
            notes: req.body.notes || game.notes
          });
      })
      .then((game) => res.status(200).send({
        status: 200,
        message: 'Game updated',
        data: game,
      }))
      .catch((error) => {
        if (error.status) {
          res.status(error.status).send(error);
        }
        else {
          res.status(500).send({
            status: 500,
            message: 'Internal server error',
            errors: [error],
          });
        }
      });
  },

  /**
   * Destroy Game by gameId
   *
   * @route DELETE /games/:gameId
   *
   * @param {*} req
   * @param {*} res
   */
  destroy(req, res) {
    return Game
      .findById(req.params.gameId)
      .then(game => {
        if (!game) {
          throw {
            status: 404,
            message: 'Game not found',
            errors: ['Game not found'],
          };
        }

        return game.destroy()
          .then(() => new Promise((resolve) => resolve(game)));
      })
      .then((game) => {
        res.status(200).send({
          status: 200,
          message: 'Game destroyed',
          data: game,
        });
      })
      .catch((error) => {
        if (error.status) {
          res.status(error.status).send(error);
        }
        else {
          res.status(500).send({
            status: 500,
            message: 'Internal server error',
            errors: [error],
          });
        }
      });
  }
};

@@ -1701,335 +1701,103 @@
     
-game_number: req.body.game_number %7C%7C game.game_number,%0A            sr_change: req.body.sr_change %7C%7C game.sr_change,%0A            rank: req.body.rank %7C%7C game.rank,%0A            streak: req.body.streak %7C%7C game.streak,%0A            map: req.body.map %7C%7C game.map,%0A            score: req.body.score %7C%7C game.score,%0A            group_siz
+rank: req.body.rank %7C%7C game.rank,%0A            map: req.body.map %7C%7C game.map,%0A            outcom
 e: r
@@ -1804,25 +1804,22 @@
 eq.body.
-group_siz
+outcom
 e %7C%7C gam
@@ -1824,119 +1824,16 @@
 ame.
-group_size,%0A            heroes: req.body.heroes %7C%7C game.heroes,%0A            notes: req.body.notes %7C%7C game.notes
+outcome,
 %0A   

update update