import { API_BASE_URL } from '@/config/serverApiConfig';
import { DEMO_LOGIN_EMAIL, DEMO_LOGIN_PASSWORD, DEMO_MODE } from '@/demo/config';

import axios from 'axios';
import errorHandler from '@/request/errorHandler';
import successHandler from '@/request/successHandler';

const getDemoApi = async () => (await import('@/demo')).demoApi;

export const login = async ({ loginData }) => {
  if (DEMO_MODE) {
    const demoApi = await getDemoApi();
    return demoApi.login({ loginData });
  }
  try {
    const response = await axios.post(
      API_BASE_URL + `login?timestamp=${new Date().getTime()}`,
      loginData
    );

    const { status, data } = response;

    successHandler(
      { data, status },
      {
        notifyOnSuccess: false,
        notifyOnFailed: true,
      }
    );
    return data;
  } catch (error) {
    return errorHandler(error);
  }
};

export const register = async ({ registerData }) => {
  if (DEMO_MODE) {
    return {
      success: true,
      result: { email: registerData?.email },
      message: 'Demo registration request captured',
    };
  }
  try {
    const response = await axios.post(API_BASE_URL + `register`, registerData);

    const { status, data } = response;

    successHandler(
      { data, status },
      {
        notifyOnSuccess: true,
        notifyOnFailed: true,
      }
    );
    return data;
  } catch (error) {
    return errorHandler(error);
  }
};

export const verify = async ({ userId, emailToken }) => {
  if (DEMO_MODE) {
    const demoApi = await getDemoApi();
    const loginResult = await demoApi.login({
      loginData: { email: DEMO_LOGIN_EMAIL, password: DEMO_LOGIN_PASSWORD },
    });
    return {
      success: true,
      result: loginResult.result,
      message: 'Demo verification completed',
    };
  }
  try {
    const response = await axios.get(API_BASE_URL + `verify/${userId}/${emailToken}`);

    const { status, data } = response;

    successHandler(
      { data, status },
      {
        notifyOnSuccess: true,
        notifyOnFailed: true,
      }
    );
    return data;
  } catch (error) {
    return errorHandler(error);
  }
};

export const resetPassword = async ({ resetPasswordData }) => {
  if (DEMO_MODE) {
    return {
      success: true,
      result: null,
      message: 'Demo password reset completed',
    };
  }
  try {
    const response = await axios.post(API_BASE_URL + `resetpassword`, resetPasswordData);

    const { status, data } = response;

    successHandler(
      { data, status },
      {
        notifyOnSuccess: true,
        notifyOnFailed: true,
      }
    );
    return data;
  } catch (error) {
    return errorHandler(error);
  }
};
export const logout = async () => {
  if (DEMO_MODE) {
    const demoApi = await getDemoApi();
    return demoApi.logout();
  }
  axios.defaults.withCredentials = true;
  try {
    // window.localStorage.clear();
    const response = await axios.post(API_BASE_URL + `logout?timestamp=${new Date().getTime()}`);
    const { status, data } = response;

    successHandler(
      { data, status },
      {
        notifyOnSuccess: false,
        notifyOnFailed: true,
      }
    );
    return data;
  } catch (error) {
    return errorHandler(error);
  }
};

//  console.log(
//    '🚀 Welcome to IDURAR ERP CRM! Did you know that we also offer commercial customization services? Contact us at hello@idurarapp.com for more information.'
//  );
