import { cleanFilters } from '@/utils/clean-filters'
import { useMutation, useQuery } from '@tanstack/vue-query'
import {
  apiCreateMissionaryMemory,
  apiDeleteMissionaryMemory,
  apiListMissionaryMemories,
  apiUpdateMissionaryMemory,
} from '@/services/api/missionary-memories'

export const missionaryMemoriesKeys = {
  all: ['missionary', 'memories'],
  list: (filters, pagination) => ['missionary', 'memories', 'list', filters, pagination],
}

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

export const useCreateMissionaryMemoryMutation = () =>
  useMutation({ mutationFn: apiCreateMissionaryMemory })

export const useUpdateMissionaryMemoryMutation = () =>
  useMutation({ mutationFn: ({ id, payload }) => apiUpdateMissionaryMemory(id, payload) })

export const useDeleteMissionaryMemoryMutation = () =>
  useMutation({ mutationFn: apiDeleteMissionaryMemory })
