const { splitSite } = require("../middlewares");
const db = require("../models");
const Breakfast = db.breakfast;

// Create a new Breakfast Item
exports.createNewBreakfast = (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 Breakfast Item
  const breakfast = new Breakfast({
    customer_id: req.body.customer_id,
		customer_name: req.body.customer_name,
		has_breakfast: req.body.has_breakfast,
		create_by: req.body.create_by,
		create_date: req.body.create_date,
		edit_history: req.body.edit_history,
		date: req.body.date,
    site
  });
  // Save breakfast Item in the database
  breakfast
    .save(breakfast)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while creating the Breakfast Record."
      });
    });
};

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

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

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