import axios from 'axios';
import { shareActionTypes } from '../../action-types/share.actions.types';

/**
 * @description: Get all active agents when sharing to send invites
 * @param {*} getAllForms
 */
export const getActiveInvitedAgents = id => async dispatch => {
  const url = `form-shares/${id}`;
  try {
    const response = await axios.get(url);
    const { success, data } = response.data;
    if (success) {
      dispatch({
        type: shareActionTypes.GET_ACTIVE_AGENT_LIST,
        payload: data,
      });
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};

export const inviteAgentToForm = (id, agentDetails) => async dispatch => {
  const url = `form-shares/${id}/agents/invite`;
  try {
    const agentResponse = await axios.post(url, agentDetails);
    const { success, data } = agentResponse.data;
    if (success) {
      dispatch({
        type: shareActionTypes.INVITE_AGENT_VIA_FORM_SHARE,
        payload: data,
      });
      return agentResponse.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};

/**
 * @description: extend data collection due date for the form
 * @param {*} id
 * @param {*} details
 */
export const extendFormDueDate = (id, details) => async () => {
  const url = `/form-shares/${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;
    }
  }
};

/**
 * Delete form shareurl
 * @param {*} id
 */
export const suspendShare = (id, action) => async () => {
  const url = `/form-shares/${id}/${action}`;
  try {
    const response = await axios.patch(url);
    const { success } = response.data;
    if (success) {
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};

/**
 * @description: Suspend an agent from accessing a form
 * @param {*} id The ID of the form to suspend agent
 * @param {*} agents : Agent to be suspended
 */
export const suspendAgentsFromShare = (id, agents) => async () => {
  const url = `/form-shares/${id}/agents/suspend`;
  try {
    const response = await axios.patch(url, agents);
    const { success } = response.data;
    if (success) {
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};

/**
 *  @description: Resume an agent from accessing a form
 * @param {*} id : Id of the form to suspend agent
 * @param {*} agents : Agents to be suspended
 */
export const resumeAgentInShare = (id, agents) => async () => {
  const url = `/form-shares/${id}/agents/resume`;
  try {
    const response = await axios.patch(url, agents);
    const { success } = response.data;
    if (success) {
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};
