'use client'

import { useQueryClient } from '@tanstack/react-query'
import { IBaseResponse } from '@/@core/types/api'
import { IPaymentMethodDetails } from '@/types/data/payment-method'
import { request } from '@/utils'
import { PAYMENT_METHODS_LIST_QUERY_KEY } from './useGetPaymentMethodsList'
import { PAYMENT_METHOD_DETAILS_QUERY_KEY } from './useGetPaymentMethodDetails'
import { useCustomMutation } from '@/hooks/api'

const invalidatePaymentMethodQueries = async (queryClient: ReturnType<typeof useQueryClient>, methodId?: string) => {
  const promises = [queryClient.invalidateQueries({ queryKey: [PAYMENT_METHODS_LIST_QUERY_KEY] })]

  if (methodId) {
    promises.push(queryClient.invalidateQueries({ queryKey: [PAYMENT_METHOD_DETAILS_QUERY_KEY, methodId] }))
  }

  await Promise.all(promises)
}

export const useTogglePaymentMethod = () => {
  const queryClient = useQueryClient()

  return useCustomMutation<string, IBaseResponse<IPaymentMethodDetails>>(
    ['payment-methods-toggle'],
    methodId =>
      request({
        url: `/payment-methods/${methodId}/toggle-active`,
        method: 'PATCH'
      }),
    {
      onSuccess: async (_, methodId) => {
        await invalidatePaymentMethodQueries(queryClient, methodId)
      }
    }
  )
}

export const useActivatePaymentMethod = () => {
  const queryClient = useQueryClient()

  return useCustomMutation<string, IBaseResponse<IPaymentMethodDetails>>(
    ['payment-methods-activate'],
    methodId =>
      request({
        url: `/payment-methods/${methodId}/activate`,
        method: 'PATCH'
      }),
    {
      onSuccess: async (_, methodId) => {
        await invalidatePaymentMethodQueries(queryClient, methodId)
      }
    }
  )
}

export const useDeactivatePaymentMethod = () => {
  const queryClient = useQueryClient()

  return useCustomMutation<string, IBaseResponse<IPaymentMethodDetails>>(
    ['payment-methods-deactivate'],
    methodId =>
      request({
        url: `/payment-methods/${methodId}/deactivate`,
        method: 'PATCH'
      }),
    {
      onSuccess: async (_, methodId) => {
        await invalidatePaymentMethodQueries(queryClient, methodId)
      }
    }
  )
}

export const useDeletePaymentMethod = () => {
  const queryClient = useQueryClient()

  return useCustomMutation<string, IBaseResponse<any>>(
    ['payment-methods-delete'],
    methodId =>
      request({
        url: `/payment-methods/${methodId}`,
        method: 'DELETE'
      }),
    {
      onSuccess: async () => {
        await invalidatePaymentMethodQueries(queryClient)
      }
    }
  )
}

export const useRestorePaymentMethod = () => {
  const queryClient = useQueryClient()

  return useCustomMutation<string, IBaseResponse<IPaymentMethodDetails>>(
    ['payment-methods-restore'],
    methodId =>
      request({
        url: `/payment-methods/${methodId}/restore`,
        method: 'PATCH'
      }),
    {
      onSuccess: async (_, methodId) => {
        await invalidatePaymentMethodQueries(queryClient, methodId)
      }
    }
  )
}
