import { useQuery } from '@tanstack/react-query'
import { PaymentMethods } from '../../types/services/paymentMethod'
import { api } from '../api'

interface PaymentMethodsQueryParams {
  page?: number
  limit?: number
  search?: string
}

export function usePaymentMethods(params: PaymentMethodsQueryParams = {}) {
  const { page = 1, limit = 10, search = '', ...filters } = params

  return useQuery<PaymentMethods>({
    queryKey: ['payment-methods', { page, limit, search, ...filters }],
    queryFn: async () => {
      const queryParams = new URLSearchParams()

      queryParams.append('page', page.toString())
      queryParams.append('limit', limit.toString())

      if (search) {
        queryParams.append('search', search)
      }

      Object.entries(filters).forEach(([key, value]) => {
        if (value !== undefined && value !== null && value !== '') {
          queryParams.append(key, value.toString())
        }
      })

      const res = await api.get(`/payment-methods?${queryParams.toString()}`)
      return res.data.data
    }
  })
}
