const db = require("../models");
const Employee = db.employee;

var bcrypt = require("bcryptjs");
const { splitSite } = require("../middlewares");
// Create and Save a new Employee (driver, distributor, admin)
exports.createEmployee = (req, res) => {
  // Validate request
  if (!req.body.username) {
    res.status(400).send({ message: "Content can not be empty!" });
    return;
  }
  const site = splitSite.findSiteNumber(req);
  // Create a Employee
  const employee = new Employee({
    username: req.body.username || req.body.email || '',
    name: req.body.name || '',
    name_cn: req.body.name_cn || '',
    email: req.body.email || '',
    password: req.body.password ? bcrypt.hashSync(req.body.password, 8) : '',
    roles: req.body.roles || [],
    mobile_phone: req.body.mobile_phone || '',
    phone: req.body.phone || '',
    home_phone: req.body.home_phone || '',
    language: req.body.language || '',
    employment_status: req.body.employment_status || '',
    status: req.body.status || 'active',
    address: req.body.address || '',
    title: req.body.title || '',
    title_cn: req.body.title_cn || '',
    firstname: req.body.firstname || '',
    lastname: req.body.lastname || '',
    department: req.body.department || '',
    birth_date: req.body.birth_date || null,
    driver_capacity: req.body.driver_capacity || null,
    date_hired: req.body.date_hired || null,
    create_by: req.body.create_by || '',
    create_date: new Date(),
    edit_by: req.body.edit_by || '',
    edit_date: new Date(),
    note: req.body.note || '',
    tags: req.body.tags || [],
    fetch_route_time: req.body.fetch_route_time || null,
    site
  });
  // Save Employee in the database
  employee
    .save(employee)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while creating the Employee."
      });
    });
};
// Retrieve all Employee from the database.
exports.getAllEmployees = (req, res) => {
  var params = req.query;
  var condition = {};
  condition = splitSite.splitSiteGet(req, condition);
  if (params.status) {
    condition.status = params.status;
  }
  if (params.role) {
    condition.roles = params.role;
  } 
  Employee.find(condition)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while retrieving employees."
      });
    });
};
// Retrieve all Active Employee from the database.
exports.getAllActiveEmployees = (req, res) => {
  var params = req.query;
  var condition = { status: 'active' };
  condition = splitSite.splitSiteGet(req, condition);
  if (params.roles) {
    condition.roles = params.roles;
  } 
  Employee.find(condition)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while retrieving employees."
      });
    });
};
// Get One Employee by Id
exports.getEmployee = (req, res) => {
  const id = req.params.id;
  Employee.findById(id)
    .then(data => {
      if (!data)
        res.status(404).send({ message: "Not found Employee with id " + id });
      else res.send(data);
    })
    .catch(err => {
      res
        .status(500)
        .send({ message: "Error retrieving Employee with id=" + id });
    });
};
// Update a Employee by the id in the request
exports.updateEmployee = (req, res) => {
  if (!req.body) {
    return res.status(400).send({
      message: "Data to update can not be empty!"
    });
  }
  const id = req.params.id;
  if (req.body.password) {
    req.body.password = bcrypt.hashSync(req.body.password, 8);
  }
  Employee.findByIdAndUpdate(id, req.body, { useFindAndModify: false })
    .then(data => {
      if (!data) {
        res.status(404).send({
          message: `Cannot update employee with id=${id}. Maybe Employee was not found!`
        });
      } else res.send({ success: true, message: "Employee was updated successfully." });
    })
    .catch(err => {
      res.status(500).send({
        success: false,
        message: "Error updating Employee with id=" + id
      });
    });
};
// Soft Delete a Employee with the specified id in the request
exports.deactivateEmployee = (req, res) => {
  
};
// Delete a Employee by id
exports.deleteEmployee = (req, res) => {

};
// Get Employees with username or email 
exports.getEmployeesWithUsernameOrEmail = (req, res) => {

};
