import { cleanFilters } from '@/utils/clean-filters'
import { useMutation, useQuery } from '@tanstack/vue-query'
import {
  apiChangeAdminConsultationStatus,
  apiGetAdminConsultations,
  apiSendAdminConsultationMessage,
  apiShowAdminConsultation,
} from '@/services/api/admin-consultations'

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

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

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

export const useSendAdminConsultationMessageMutation = () =>
  useMutation({
    mutationFn: ({ id, payload }) => apiSendAdminConsultationMessage(id, payload),
  })

export const useChangeAdminConsultationStatusMutation = () =>
  useMutation({
    mutationFn: ({ id, payload }) => apiChangeAdminConsultationStatus(id, payload),
  })
