const { splitSite } = require("../middlewares");
const db = require("../models");
const Snack = db.snack;

// Create a new Snack Item
exports.createNewSnack = (req, res) => {
  // Validate request
  if (!req.body.customer_id) {
    res.status(400).send({ message: "Content can not be empty!" });
    return;
  }
  const site = splitSite.findSiteNumber(req);
	// Create an Snack Item
  const snack = new Snack({
    customer_id: req.body.customer_id,
		customer_name: req.body.customer_name,
		has_snack: req.body.has_snack,
		create_by: req.body.create_by,
		create_date: req.body.create_date,
		edit_history: req.body.edit_history,
		date: req.body.date,
    site
  });
  // Save snack Item in the database
  snack
    .save(snack)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while creating the Snack Record."
      });
    });
};

// Retrive all Snack Records from database.
exports.getAllSnacks = (req, res) => {
	var params = req.query;
	var condition = {};
	if (params.date) {
		condition.date = params.date;
	}
	 
	condition = splitSite.splitSiteGet(req, condition);
	Snack.find(condition)
		.then(data => {
			res.send(data);
		})
		.catch(err => {
			res.status(500).send({
				message:
					err.message || "Some error occurred while retrieving Snacks."
			});
		});
};

// Get One Event by Id
// exports.getEvent = (req, res) => {
//   const id = req.params.id;
//   CalendarEvent.findById(id)
//     .then(data => {
//       if (!data)
//         res.status(404).send({ message: "Not found Event with id " + id });
//       else res.send(data);
//     })
//     .catch(err => {
//       res
//         .status(500)
//         .send({ message: "Error retrieving Event with id=" + id });
//     });
// };

// Update a Snack by the id in the request
exports.updateSnack = (req, res) => {
  if (!req.body) {
    return res.status(400).send({
      message: "Data to update can not be empty!"
    });
  }
  const id = req.params.id;
  Snack.findByIdAndUpdate(id, req.body, { useFindAndModify: false })
    .then(data => {
      if (!data) {
        res.status(404).send({
          message: `Cannot update Snack with id=${id}. Maybe Snack was not found!`
        });
      } else res.send({ success: true, message: "Snack was updated successfully." });
    })
    .catch(err => {
      res.status(500).send({
        success: false,
        message: "Error updating Snack with id=" + id
      });
    });
};

// Delete a Snack by id
exports.deleteSnack= (req, res) => {
  const id = req.params.id;
  Snack.findByIdAndRemove(id)
    .then(data => {
      if (!data) {
        res.status(404).send({
          message: `Cannot delete Snack with id=${id}. Maybe Snack was not found!`
        });
      } else {
        res.send({
          message: "Snack was deleted successfully!"
        });
      }
    })
    .catch(err => {
      res.status(500).send({
        message: "Could not delete Snack with id=" + id
      });
    });
};