const db = require("../models");
const FingerprintAttendance = db.fingerprint_attendance;
const { splitSite } = require("../middlewares");

// Create and Save a new Fingerprint Attendance record
exports.createFingerprintAttendance = (req, res) => {
  // Validate request
  if (!req.body.employee) {
    res.status(400).send({ message: "Employee is required!" });
    return;
  }

  const site = splitSite.findSiteNumber(req);
  // Create a Fingerprint Attendance record
  const fingerprintAttendance = new FingerprintAttendance({
    employee: req.body.employee,
    finger_print: req.body.finger_print || '',
    finger_print_time: req.body.finger_print_time || new Date(),
    create_date: req.body.create_date || new Date(),
    create_by: req.body.create_by || '',
    update_date: req.body.update_date || new Date(),
    update_by: req.body.update_by || '',
    note: req.body.note || '',
    site: req.body.site
  });

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

// Retrieve all Fingerprint Attendance records from the database
exports.getAllFingerprintAttendances = (req, res) => {
  var params = req.query;
  var condition = {};
  condition = splitSite.splitSiteGet(req, condition);

  // Add filters based on query parameters
  if (params.employee) {
    condition.employee = params.employee;
  }
  if (params.finger_print) {
    condition.finger_print = params.finger_print;
  }
  if (params.create_by) {
    condition.create_by = params.create_by;
  }
  if (params.site) {
    condition.site = params.site;
  }

  FingerprintAttendance.find(condition)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while retrieving fingerprint attendance records."
      });
    });
};

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

// Update a Fingerprint Attendance record by the id in the request
exports.updateFingerprintAttendance = (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_date and update_by to the request body
  req.body.update_date = new Date();
  
  FingerprintAttendance.findByIdAndUpdate(id, req.body, { useFindAndModify: false })
    .then(data => {
      if (!data) {
        res.status(404).send({
          message: `Cannot update fingerprint attendance record with id=${id}. Maybe Fingerprint Attendance record was not found!`
        });
      } else res.send({ success: true, message: "Fingerprint Attendance record was updated successfully." });
    })
    .catch(err => {
      res.status(500).send({
        success: false,
        message: "Error updating Fingerprint Attendance record with id=" + id
      });
    });
};

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

// Get Fingerprint Attendance records by employee
exports.getFingerprintAttendancesByEmployee = (req, res) => {
  const employee = req.params.employee;
  var params = req.query;
  var condition = { employee: employee };
  condition = splitSite.splitSiteGet(req, condition);

  // Add additional filters
  if (params.start_date && params.end_date) {
    condition.finger_print_time = { $gte: new Date(params.start_date), $lte: new Date(params.end_date) };
  }
  if (params.site) {
    condition.site = params.site;
  }

  FingerprintAttendance.find(condition)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while retrieving fingerprint attendance records for employee " + employee
      });
    });
};

// Get Fingerprint Attendance records by date range
exports.getFingerprintAttendancesByDateRange = (req, res) => {
  var params = req.query;
  
  if (!params.start_date || !params.end_date) {
    res.status(400).send({ message: "Start date and end date are required!" });
    return;
  }

  var condition = {
    finger_print_time: { $gte: new Date(params.start_date), $lte: new Date(params.end_date) }
  };
  condition = splitSite.splitSiteGet(req, condition);

  // Add additional filters
  if (params.employee) {
    condition.employee = params.employee;
  }
  if (params.site) {
    condition.site = params.site;
  }

  FingerprintAttendance.find(condition)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while retrieving fingerprint attendance records for date range."
      });
    });
}; 