import axios from 'axios';
import { agentTypes } from '../../action-types/agent-action.types';

// AGENTS ENDPOINTS
/**
 * @description: Add new agents
 * @param {*} details
 */
export const addAgents = details => async () => {
  const url = '/agents/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;
    }
  }
};

/**
 * @function: get all onboarded agents
 */
export const getAllAgents = page => async dispatch => {
  const url = `agents?skip=${(page - 1) * 20}&limit=20`;
  try {
    const response = await axios.get(url);
    const { success, data } = response.data;
    if (success) {
      dispatch({
        type: agentTypes.GET_ALL_AGENTS,
        payload: data,
      });
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};

export const deleteAnAgent = agent => async dispatch => {
  const url = 'agents/remove';
  try {
    const response = await axios.post(url, agent);
    const { success, data } = response.data;
    if (success) {
      dispatch({
        type: agentTypes.DELETE_AN_AGENT,
        payload: data,
      });
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};

export const disableAgent = id => async dispatch => {
  const url = `/agents/${id}/disable`;
  try {
    const response = await axios.patch(url);
    const { success, data } = response.data;
    if (success) {
      dispatch({
        type: agentTypes.DISABLE_AGENT,
        payload: data,
      });
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};

export const searchAgent = query => async dispatch => {
  const url = `agents/search?q=${query}`;
  try {
    const response = await axios.get(url);
    const { success, data } = response.data;
    if (success) {
      dispatch({
        type: agentTypes.GET_SEARCHED_AGENTS,
        payload: data,
      });
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};

// AGENT GROUP ENDPOINTS
export const addNewAgentGroup = details => async () => {
  const url = '/agent-groups';
  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 getAllNewAgentGroup = (
  page,
  limit,
  callback
) => async dispatch => {
  const url = `agent-groups?skip=${(page - 1) * 20}&limit=${limit}`;
  try {
    const response = await axios.get(url);
    const { success, data } = response.data;
    if (success) {
      dispatch({
        type: agentTypes.GET_ALL_NEW_AGENT_GROUP,
        payload: data,
      });
      callback();
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};

export const searchAgentGroup = query => async dispatch => {
  const url = `agent-groups/search?q=${query}`;
  try {
    const response = await axios.get(url);
    const { success, data } = response.data;
    if (success) {
      dispatch({
        type: agentTypes.GET_SEARCHED_AGENT_GROUP,
        payload: data,
      });
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};

export const updateAgentGroup = (id, agentDetails) => async dispatch => {
  const url = `/agent-groups/${id}`;
  try {
    const response = await axios.put(url, agentDetails);
    const { success, data } = response.data;
    if (success) {
      dispatch({
        type: agentTypes.UPDATE_AGENT_GROUP_NAME,
        payload: data,
      });
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};

export const deleteAnAgentGroup = id => async dispatch => {
  const url = `agent-groups/${id}`;
  try {
    const response = await axios.delete(url);
    const { success, data } = response.data;
    if (success) {
      dispatch({
        type: agentTypes.DELETE_AN_AGENT_GROUP,
        payload: data,
      });
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};

export const addAgentsToAgentGroup = id => async dispatch => {
  const url = `/agent-groups/${id}/add-agents`;
  try {
    const response = await axios.patch(url);
    const { success, data } = response.data;
    if (success) {
      dispatch({
        type: agentTypes.ADD_AGENT_TO_AN_AGENT_GROUP,
        payload: data,
      });
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};

export const getAllAgentsInAGroup = (id, agents) => async dispatch => {
  const url = `/agent-groups/${id}`;
  try {
    const response = await axios.get(url, agents);
    const { success, data } = response.data;
    if (success) {
      dispatch({
        type: agentTypes.GET__ALL_AGENT_IN_A_GROUP,
        payload: data,
      });
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};

export const removeAgentInGroup = (id, agents) => async dispatch => {
  const url = `agent-groups/${id}/remove-agents`;
  try {
    const response = await axios.patch(url, agents);
    const { success, data } = response.data;
    if (success) {
      dispatch({
        type: agentTypes.REMOVE_AGENT_FROM_GROUP,
        payload: data,
      });
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};
