import axios from 'axios';
import { authActionTypes } from '../../action-types/auth-action.types';

/**
 * @description Action creator for login operation
 * @param {email, password} loginDetails
 */
export const login = loginDetails => async dispatch => {
  const url = '/auth/login';
  try {
    const response = await axios.post(url, loginDetails);
    const { success, data } = response.data;
    if (success) {
      sessionStorage.setItem('@__useform__', JSON.stringify(data));
      dispatch({
        type: authActionTypes.GET_USER,
        payload: data,
      });
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};

/**
 * @description Action creator for login into workspace
 * @param {token, workspaceId}
 */
export const loginToWorkspace = (token, workspace) => async dispatch => {
  const loginDetails = {
    token,
    workspace,
  };
  const url = '/auth/select-workspace';
  try {
    const response = await axios.post(url, loginDetails);
    const { success, data } = response.data;
    if (success) {
      sessionStorage.setItem('@__useform__', JSON.stringify(data));
      dispatch({
        type: authActionTypes.GET_USER,
        payload: data,
      });
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};

/**
 * @description get all workspaces associated with this email
 * @param {token, email} getWorkspaces
 */
export const getWorkspaces = (token, email) => async dispatch => {
  const url = '/auth/workspaces';
  const config = { headers: { Authorization: `Bearer ${token}` } };
  const loginDetails = {
    email,
  };
  try {
    const response = await axios.post(url, loginDetails, config);
    const { success, data } = response.data;
    if (success) {
      dispatch({
        type: authActionTypes.GET_WORKSPACES,
        payload: data,
      });
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};

/**
 * @description Action creator for signup operation
 * @param {email, workspaceName, password} signup details
 */
export const signup = signupDetails => async () => {
  const url = '/auth/signup';
  try {
    const response = await axios.post(url, signupDetails);
    const { success } = response.data;
    if (success) {
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};

/**
 * @description Verify a registered email
 * @param {token} verifyEmail details
 */
export const verifyEmail = token => async () => {
  const url = '/auth/verify-email';
  try {
    const response = await axios.post(url, token);
    const { success } = response.data;
    if (success) {
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};

/**
 * @description Forgot password request
 * @param {email} already registered email
 */
export const forgotPassword = email => async () => {
  const forgotPasswordDetails = email;
  const url = '/auth/forgot-password';
  try {
    const response = await axios.post(url, forgotPasswordDetails);
    const { success } = response.data;
    if (success) {
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};

/**
 * @description Reset password request
 * @param {email} already registered email
 */
export const resetPassword = (token, newPassword) => async () => {
  // let formattedToken = token.split('=').pop();
  const resePasswordDetails = { token, newPassword };
  const url = '/auth/reset-password';
  try {
    const response = await axios.post(url, resePasswordDetails);
    const { success } = response.data;
    if (success) {
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};

/**
 * @description: switch to another workspace
 * @param {id} object id of the workspace to select
 */
export const switchWorkspace = id => async () => {
  const url = `/auth/switch/${id}`;
  try {
    const response = await axios.post(url);
    const { success, data } = response.data;
    if (success) {
      sessionStorage.setItem('@__useform__', JSON.stringify(data));
      return response.data;
    }
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};
