const { splitSite } = require("../middlewares");
const db = require("../models");
const DailyRoutesTemplate = db.dailyRoutesTemplate;

// Create and Save a new Daily Routes Template
exports.createDailyRoutesTemplate = (req, res) => {
  // Validate request
  if (!req.body.name) {
    res.status(400).send({ message: "Content can not be empty!" });
    return;
  }
  const site = splitSite.findSiteNumber(req);
  // Create a Daily Routes Template
  const dailyRoutesTemplate = new DailyRoutesTemplate({
    name: req.body.name,
    template_date: req.body.template_date,
    routes: req.body.routes || [],
    site,
    create_by: req.body.create_by || '',
    create_date: new Date()
  });
  // Save Daily Routes Template in the database
  dailyRoutesTemplate
    .save(dailyRoutesTemplate)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while creating the Daily Routes Template."
      });
    });
};

// Retrieve all Daily Routes Templates from the database
exports.getAllDailyRoutesTemplates = (req, res) => {
  var params = req.query;
  var condition = {};
  condition = splitSite.splitSiteGet(req, condition);
  
  if (params.template_date) {
    condition.template_date = params.template_date;
  }
  
  DailyRoutesTemplate.find(condition)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while retrieving Daily Routes Templates."
      });
    });
};

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

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

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

