import { cleanFilters } from '@/utils/clean-filters'
import { useMutation, useQuery } from '@tanstack/vue-query'
import {
  apiChangeMissionaryRequestStatus,
  apiListMissionaryRequests,
  apiShowMissionaryRequest,
} from '@/services/api/missionary-requests'

export const missionaryRequestsKeys = {
  all: ['missionary', 'requests'],
  list: (filters, pagination) => ['missionary', 'requests', 'list', filters, pagination],
  detail: (id) => ['missionary', 'requests', 'detail', id],
}

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

export const useMissionaryRequestQuery = (idRef, options = {}) =>
  useQuery({
    queryKey: ['missionary', 'requests', 'detail', idRef],
    queryFn: () => apiShowMissionaryRequest(idRef.value),
    select: (response) => response?.data ?? response,
    ...options,
  })

export const useChangeMissionaryRequestStatusMutation = () =>
  useMutation({
    mutationFn: ({ id, payload }) => apiChangeMissionaryRequestStatus(id, payload),
  })
