import { cleanFilters } from '@/utils/clean-filters'
import { useMutation, useQuery } from '@tanstack/vue-query'
import {
  apiChangeAdminMissionaryRequestStatus,
  apiListAdminMissionaryRequests,
} from '@/services/api/admin-missionary-requests'

export const adminMissionaryRequestsKeys = {
  all: ['admin', 'missionary-requests'],
  list: (filters, pagination) => ['admin', 'missionary-requests', 'list', filters, pagination],
}

export const useAdminMissionaryRequestsListQuery = (filtersRef, paginationRef, options = {}) =>
  useQuery({
    queryKey: ['admin', 'missionary-requests', 'list', filtersRef, paginationRef],
    queryFn: () =>
      apiListAdminMissionaryRequests({ ...cleanFilters(filtersRef.value), ...paginationRef.value }),
    select: (response) => ({
      data: response?.data?.items ?? response?.data ?? [],
      meta: response?.data?.meta ?? response?.meta,
    }),
    ...options,
  })

export const useChangeAdminMissionaryRequestStatusMutation = () =>
  useMutation({
    mutationFn: ({ id, payload }) => apiChangeAdminMissionaryRequestStatus(id, payload),
  })
