const { splitSite } = require("../middlewares");
const db = require("../models");
const Seating = db.seating;

// Create a new Seating Item
exports.createNewSeating = (req, res) => {
  // Validate request
  if (!req.body.seating_assignment) {
    res.status(400).send({ message: "Assignment can not be empty!" });
    return;
  }
  const site = splitSite.findSiteNumber(req);
	// Create an Seating Item
  const seating = new Seating({
		seating_assignment: req.body.seating_assignment,
		date: req.body.date,
		create_by: req.body.create_by,
		create_date: req.body.create_date,
		update_by: req.body.update_by,
		update_date: req.body.update_date,
    site
  });
  // Save seating Item in the database
  seating
    .save(seating)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while creating the Seating Record."
      });
    });
};

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

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

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