import { cleanFilters } from '@/utils/clean-filters'
import { useMutation, useQuery } from '@tanstack/vue-query'
import {
  apiChangeAdminTicketStatus,
  apiGetAdminTickets,
  apiSendAdminTicketMessage,
  apiShowAdminTicket,
} from '@/services/api/admin-tickets'

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

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

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

export const useSendAdminTicketMessageMutation = () =>
  useMutation({
    mutationFn: ({ id, payload }) => apiSendAdminTicketMessage(id, payload),
  })

export const useChangeAdminTicketStatusMutation = () =>
  useMutation({
    mutationFn: ({ id, payload }) => apiChangeAdminTicketStatus(id, payload),
  })
