import axios from 'axios';
import { workspaceActionTypes } from '../../action-types/workspace-actions.type';
/**
 * @description: Get all workspaces that the admin belongs to
 * @param {*} getAllWorkspace
 */

export const getWorkspaces = limit => async dispatch => {
  let url = '';

  if (limit) {
    url = `/workspaces?limit=${limit}`;
  } else {
    url = '/workspaces';
  }

  try {
    const response = await axios.get(url);
    const { success, data } = response.data;

    if (success) {
      dispatch({
        type: workspaceActionTypes.GET_ALL_WORKSPACES,
        payload: data,
      });
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};
/**
 * @description: get the details of the current workspace that the user is in
 * @param {*} getCurrentWorkpsace
 */

export const getCurrentWorkpace = () => async dispatch => {
  const url = '/workspaces/current';

  try {
    const response = await axios.get(url);
    const { success, data } = response.data;

    if (success) {
      dispatch({
        type: workspaceActionTypes.GET_CURRENT_WORSPACE,
        payload: data,
      });
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};
/**
 * @description: create new workspace
 * @param {workspaceDetails} workspaceDetails
 */

export const createWorkspace = workspaceDetails => async () => {
  const url = '/workspaces';

  try {
    const response = await axios.post(url, workspaceDetails);
    const { success } = response.data;

    if (success) {
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};
/**
 * @description: Get user roles
 * @param {category} getRoles
 */

export const getRoles = category => async dispatch => {
  let url = '';

  if (category) {
    url = `/roles?category=${category}`;
  } else {
    url = '/roles';
  }

  try {
    const response = await axios.get(url);
    const { success, data } = response.data;

    if (success) {
      dispatch({
        type: workspaceActionTypes.GET_ROLES,
        payload: data,
      });
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};
export const inviteCollaborators = details => async () => {
  const url = '/users/invite';

  try {
    const response = await axios.post(url, details);
    const { success } = response.data;

    if (success) {
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};
export const updateWorkspace = (id, details) => async () => {
  const url = `/workspaces/${id}`;

  try {
    const response = await axios.put(url, details);
    const { success } = response.data;

    if (success) {
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};
/**
 * @description: send an Id of the workspace to be deleteds
 * @param {*} id
 */

export const deleteWorkspace = id => async () => {
  const url = `/workspaces/${id}`;

  try {
    const response = await axios.del(url);
    const { success } = response.data;

    if (success) {
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};
