const mongoose = require("mongoose");

const Articlefeedback = mongoose.model("articleFeedback");

/* * Gets called from the index.router.js
 * Updates the article rating 
 */
module.exports.rating = (req, res, next) => {
    // var MongoClient = require("mongodb").MongoClient;
    // var url = "mongodb://localhost:27017/EnvoScholar";
    Articlefeedback.findOneAndUpdate(
         { title: req.body.title },
         {$set: { rating: req.body.rating}}, {upsert: true},
         function(err, savedRating) {
           if (err) {
            res.sendStatus(404); 
           } else {
            res.json(savedRating);
           }
         }
       );
}

/* * Gets called from the index.router.js
* Returns the rating number */
module.exports.getRating = (req, res, next) => {
  Articlefeedback.findOne({ title: req.body.title }, (err, articlefeedback) => {
    if (!articlefeedback) {
        Articlefeedback.create({ title: req.body.title, rating : 0})
        //rate.save;
        var initValue = {rating : 0};
        return res.status(200).json({
            status: true,
            articlefeedback : initValue
        });
    /* return res
        .status(404)
        .json({ status: false, message: "User rating not found." });*/
    } else {
      return res.status(200).json({
        status: true,
        articlefeedback: articlefeedback
      });
    }
  });
};

/* * Gets called from the index.router.js
* Returns the number of interactions per query */
module.exports.articleRatings = (req, res, next) => {
  Articlefeedback.aggregate([
    {
        '$group': {
        '_id': '$title', 
        'count': {
          '$sum': '$rating'
        }
      }
    },{
      '$sort' : { 'count' : -1 }
    }
  ]).exec((err, populatedResult) => {
      if(err) {
        res.sendStatus(404); 
      } else {
          //use this log to debug the JSON ouput
          // console.log(JSON.stringify(populatedResult));
          //res.status(200).send(populatedResult);
          return res.status(200).json({
              status: true,
              globalRatings: populatedResult
          });
      };
    });
};