import { notifications } from '@mantine/notifications'
import { get, post } from './apiService'
import { _NOTIFICATION_MESS } from './notificationMess'

/* eslint-disable @typescript-eslint/no-explicit-any */
/**
 * @author Joseph Le
 * @param url API url
 * @param data request payload
 * @param fnc function to refresh data
 * @param config custom config
 */
export const create = async (
  url: string,
  data: any,
  fnc?: () => void,
  config?: any,
) => {
  try {
    let res
    if (config) {
      res = await post(url, data, config)
    } else {
      res = await post(url, data)
    }

    if (res.status) {
      notifications.show({
        title: 'Success',
        message: _NOTIFICATION_MESS.create_success,
        color: 'green',
      })

      fnc && fnc()
    }
    if (res.status === false) {
      notifications.show({
        title: 'Error',
        message: res.message ?? _NOTIFICATION_MESS.create_error,
        color: 'red',
      })
    }

    return res.status
  } catch (error: any) {
    console.log(error)
    if (error.response.status === 422) {
      const errorMess = error.response.data.message
      notifications.show({
        title: 'Error',
        message: errorMess,
        color: 'red',
      })
    }
  }
}

/**
 * @author Joseph Le
 * @param url API url
 * @param data request payload
 * @param fnc function to refresh data
 * @param config custom config
 */
export const update = async (
  url: string,
  data: any,
  fnc?: () => void,
  config?: any,
) => {
  try {
    let res
    if (config) {
      res = await post(url, data, config)
    } else {
      res = await post(url, data)
    }

    if (res.status) {
      notifications.show({
        title: 'Success',
        message: _NOTIFICATION_MESS.update_success,
        color: 'green',
      })
      fnc && fnc()
    }
    if (res.status === false) {
      notifications.show({
        title: 'Error',
        message: res.message ?? _NOTIFICATION_MESS.update_error,
        color: 'red',
      })
    }

    return res.status
  } catch (error: any) {
    console.log(error)
    if (error.response.status === 422) {
      const errorMess = error.response.data.message
      notifications.show({
        title: 'Error',
        message: errorMess,
        color: 'red',
      })
    }
  }
}

/**
 * @author Joseph Le
 * @param url API url
 * @param data request payload
 * @param fnc function to refresh data
 */
export const Xdelete = async (url: string, data: any, fnc?: () => void) => {
  try {
    const res = await get(url, data)

    if (res.status) {
      notifications.show({
        title: 'Success',
        message: _NOTIFICATION_MESS.delete_success,
        color: 'green',
      })
      fnc && fnc()
    }
    if (res.status === false) {
      notifications.show({
        title: 'Error',
        message: res.message ?? _NOTIFICATION_MESS.delete_error,
        color: 'red',
      })
    }

    return res.status
  } catch (error: any) {
    console.log(error)
    if (error?.status == false) {
      notifications.show({
        title: 'Error',
        message: error.message ?? _NOTIFICATION_MESS.delete_error,
        color: 'red',
      })
    }
    if (error.response.status === 422) {
      const errorMess = error.response.data.message
      notifications.show({
        title: 'Error',
        message: errorMess,
        color: 'red',
      })
    }
  }
}
