const { splitSite } = require("../middlewares");
const db = require("../models");
const SignatureRequest = db.signature_request;

// Create a new Signature Request
exports.createNewSignatureRequest = (req, res) => {
  // Validate request
  if (!req.body.route_id) {
    res.status(400).send({ message: "Content can not be empty!" });
    return;
  }
  const site = splitSite.findSiteNumber(req);
	// Create an Signature Request
  const signatureRequest = new SignatureRequest({
    driver_id: req.body.driver_id,
    driver_name: req.body.driver_name,
    route_id: req.body.route_id,
    route_date: req.body.route_date,
    route_name: req.body.route_name,
    status: req.body.status || 'active',
		create_by: req.body.create_by,
		create_date: req.body.create_date,
    site
  });
  // Save Signature Request in the database
  signatureRequest
    .save(signatureRequest)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while creating the Signature Request Record."
      });
    });
};

// Retrive all SignatureRequest from database.
exports.getAllSignatureRequests = (req, res) => {
	var params = req.query;
	var condition = {};
	if (params.route_date) {
		condition.route_date = params.route_date;
	}
  if (params.route_id) {
    condition.route_id = params.route_id;
  }
  if (params.driver_id) {
    condition.driver_id = params.driver_id;
  }
	 
	condition = splitSite.splitSiteGet(req, condition);
	SignatureRequest.find(condition)
		.then(data => {
			res.send(data);
		})
		.catch(err => {
			res.status(500).send({
				message:
					err.message || "Some error occurred while retrieving Signature Requests."
			});
		});
};

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

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

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