const { splitSite } = require("../middlewares");
const db = require("../models");
const AttendanceNote = db.attendance_note;

// Create a new Attendance Note
exports.createNewAttendanceNote = (req, res) => {
  // Validate request
  if (!req.body.slogan || !req.body.introduction) {
    res.status(400).send({ message: "Slogan and introduction can not be empty!" });
    return;
  }
  const site = splitSite.findSiteNumber(req);
  
  // Create an Attendance Note
  const attendanceNote = new AttendanceNote({
    slogan: req.body.slogan,
    introduction: req.body.introduction,
    image: req.body.image || '',
    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 Attendance Note in the database
  attendanceNote
    .save(attendanceNote)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while creating the Attendance Note."
      });
    });
};

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

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

// Update an Attendance Note by the id in the request
exports.updateAttendanceNote = (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 || ''
  };
  
  AttendanceNote.findByIdAndUpdate(id, updateData, { useFindAndModify: false })
    .then(data => {
      if (!data) {
        res.status(404).send({
          message: `Cannot update Attendance Note with id=${id}. Maybe Attendance Note was not found!`
        });
      } else res.send({ success: true, message: "Attendance Note was updated successfully." });
    })
    .catch(err => {
      res.status(500).send({
        success: false,
        message: "Error updating Attendance Note with id=" + id + ": " + (err.message || "")
      });
    });
};

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