import { cleanFilters } from '@/utils/clean-filters'
import { useMutation, useQuery } from '@tanstack/vue-query'
import {
  apiChangeCounselorTicketStatus,
  apiGetCounselorTickets,
  apiSendCounselorTicketMessage,
  apiShowCounselorTicket,
} from '@/services/api/counselor-tickets'

export const counselorTicketsKeys = {
  all: ['counselor', 'tickets'],
  list: (filters, pagination) => ['counselor', 'tickets', 'list', filters, pagination],
  detail: (id) => ['counselor', 'tickets', 'detail', id],
}

export const useCounselorTicketsListQuery = (filtersRef, paginationRef, options = {}) =>
  useQuery({
    queryKey: ['counselor', 'tickets', 'list', filtersRef, paginationRef],
    queryFn: () =>
      apiGetCounselorTickets({ ...cleanFilters(filtersRef.value), ...paginationRef.value }),
    select: (response) => ({
      data: response?.data ?? [],
      meta: response?.meta,
    }),
    ...options,
  })

export const useCounselorTicketQuery = (idRef, options = {}) =>
  useQuery({
    queryKey: ['counselor', 'tickets', 'detail', idRef],
    queryFn: () => apiShowCounselorTicket(idRef.value),
    select: (response) => response?.data ?? response,
    ...options,
  })

export const useSendCounselorTicketMessageMutation = () =>
  useMutation({
    mutationFn: ({ id, payload }) => apiSendCounselorTicketMessage(id, payload),
  })

export const useChangeCounselorTicketStatusMutation = () =>
  useMutation({
    mutationFn: ({ id, payload }) => apiChangeCounselorTicketStatus(id, payload),
  })
