import http from "../http-common";
const getAllEmployees = (role, status) => {
  const params = {};
  if (role) {
    params.roles = role;
  }
  if (status) {
    params.status = status;
  }
  return http.get('/employees', {
    params
  });
};

const getEmployee = (id) => {
  return http.get(`/employees/${id}`);
}

const createNewEmployee = (data) => {
  data.status = 'active';
	return http.post('/employees', data);
};

const updateEmployee = (id, data) => {
  return http.put(`/employees/${id}`, data);
}

const deleteEmployee = (id, data) => {
  data.status = 'inactive';
  return http.put(`/employees/${id}`, data);
}

const uploadEmployeeFile = (data, employeeId, name, fileType) => {
  return http.post(`/files/upload-physical?objectId=${employeeId}&name=${name}&fileType=${fileType}&model=employee`, data)
}

const getAllEmployeeFiles = (employeeId, name, fileType) => {
  return http.get(`/files/uploadedDocs/employee/${employeeId}/type/${fileType}/name/${name}`)
}

const getExternalEmployeesList = () => {
  return http.post('/employees/external/list');
};

const getExternalUserPermission = (externalUserId, site) => {
  return http.get('/employees/external/permissions', {
    params: {
      external_user_id: externalUserId,
      site
    }
  });
};

const getExternalUserPermissionsList = (site) => {
  return http.get('/employees/external/permissions-list', {
    params: {
      site
    }
  });
};

const saveExternalUserPermission = (data) => {
  return http.post('/employees/external/permissions', data);
};

const revokeExternalUserPermission = (externalUserId, site) => {
  return http.delete('/employees/external/permissions', {
    params: {
      external_user_id: externalUserId,
      site
    }
  });
};

const validatePassword = (password = '') => {
  const lowercaseRegex = /[a-z]/;
  const uppercaseRegex = /[A-Z]/;
  const specialCharRegex = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/;
  if (password?.length < 8) {
    return false;
  }

  if (!lowercaseRegex.test(password)) {
    return false; // Missing lowercase letter
  }

  if (!uppercaseRegex.test(password)) {
    return false; // Missing uppercase letter
  }

  if (!specialCharRegex.test(password)) {
    return false; // Missing special character
  }

  return true; // Password meets criteria
}

export const EmployeeService = {
  getAllEmployees,
  createNewEmployee,
  updateEmployee,
  deleteEmployee,
  getEmployee,
  validatePassword,
  uploadEmployeeFile,
  getAllEmployeeFiles,
  getExternalEmployeesList,
  getExternalUserPermission,
  getExternalUserPermissionsList,
  saveExternalUserPermission,
  revokeExternalUserPermission
};
