import { cleanFilters } from '@/utils/clean-filters'
import { useMutation, useQuery } from '@tanstack/vue-query'
import {
  apiChangeAdminServiceStatus,
  apiGetAdminServices,
  apiSendAdminServiceMessage,
  apiShowAdminService,
} from '@/services/api/admin-services'

export const adminServicesKeys = {
  all: ['admin', 'services'],
  list: (filters, pagination) => ['admin', 'services', 'list', filters, pagination],
  detail: (id) => ['admin', 'services', 'detail', id],
}

export const useAdminServicesListQuery = (filtersRef, paginationRef, options = {}) =>
  useQuery({
    queryKey: ['admin', 'services', 'list', filtersRef, paginationRef],
    queryFn: () =>
      apiGetAdminServices({ ...cleanFilters(filtersRef.value), ...paginationRef.value }),
    select: (response) => ({
      data: response?.data ?? [],
      meta: response?.meta,
    }),
    ...options,
  })

export const useAdminServiceQuery = (idRef, options = {}) =>
  useQuery({
    queryKey: ['admin', 'services', 'detail', idRef],
    queryFn: () => apiShowAdminService(idRef.value),
    select: (response) => response?.data ?? response,
    ...options,
  })

export const useSendAdminServiceMessageMutation = () =>
  useMutation({
    mutationFn: ({ id, payload }) => apiSendAdminServiceMessage(id, payload),
  })

export const useChangeAdminServiceStatusMutation = () =>
  useMutation({
    mutationFn: ({ id, payload }) => apiChangeAdminServiceStatus(id, payload),
  })
