const { splitSite } = require("../middlewares");
const db = require("../models");
const CalendarEventRecur = db.calendar_event_recur;

// Create a new Recurring Event Rule
exports.create = (req, res) => {
  if (!req.body.rrule || !req.body.start_repeat_date) {
    res.status(400).send({ message: "rrule and start_repeat_date are required!" });
    return;
  }
  const site = splitSite.findSiteNumber(req);
  const recur = new CalendarEventRecur({
    title: req.body.title,
    type: req.body.type,
    description: req.body.description,
    color: req.body.color,
    status: req.body.status || 'active',
    target_type: req.body.target_type,
    target_uuid: req.body.target_uuid,
    target_name: req.body.target_name,
    event_location: req.body.event_location,
    event_reminder_type: req.body.event_reminder_type,
    meal_type: req.body.meal_type,
    activity_category: req.body.activity_category,
    ingredients: req.body.ingredients,
    rrule: req.body.rrule,
    start_repeat_date: req.body.start_repeat_date,
    end_repeat_date: req.body.end_repeat_date,
    indefinite_repeat: req.body.indefinite_repeat || false,
    create_by: req.body.create_by,
    create_date: req.body.create_date,
    edit_by: req.body.edit_by,
    edit_date: req.body.edit_date,
    edit_history: req.body.edit_history,
    site
  });
  recur
    .save(recur)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message: err.message || "Some error occurred while creating the Recurring Event Rule."
      });
    });
};

// Get all active recurring event rules
exports.getAll = (req, res) => {
  var condition = { status: 'active' };
  condition = splitSite.splitSiteGet(req, condition);
  CalendarEventRecur.find(condition)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message: err.message || "Some error occurred while retrieving Recurring Event Rules."
      });
    });
};

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

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

// Disable (set status to inactive)
exports.disable = (req, res) => {
  const id = req.params.id;
  CalendarEventRecur.findByIdAndUpdate(id, { ...req.body, status: 'inactive' }, { useFindAndModify: false })
    .then(data => {
      if (!data) {
        res.status(404).send({
          message: `Cannot disable Recurring Event Rule with id=${id}. Maybe it was not found!`
        });
      } else res.send({ success: true, message: "Recurring Event Rule was disabled successfully." });
    })
    .catch(err => {
      res.status(500).send({
        success: false,
        message: "Error disabling Recurring Event Rule with id=" + id
      });
    });
};

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