const { splitSite } = require("../middlewares");
const db = require("../models");
const Carousel = db.carousel;

// Create a new Carousel
exports.createNewCarousel = (req, res) => {
  const site = splitSite.findSiteNumber(req);
  
  // Create a Carousel
  const carousel = new Carousel({
    status: req.body.status || 'active',
    site,
    create_date: new Date(),
    create_by: req.body.create_by || '',
    update_date: new Date(),
    update_by: req.body.update_by || ''
  });

  // Save Carousel in the database
  carousel
    .save(carousel)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while creating the Carousel."
      });
    });
};

// Retrieve all Carousels from the database
exports.getAllCarousels = (req, res) => {
  var condition = {};
  
  // Filter by status if provided
  if (req.query.status) {
    condition.status = req.query.status;
  }
  
  condition = splitSite.splitSiteGet(req, condition);
  Carousel.find(condition)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while retrieving Carousels."
      });
    });
};

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

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

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