import { cleanFilters } from '@/utils/clean-filters'
import { useMutation, useQuery } from '@tanstack/vue-query'
import {
  apiChangePendingStudentStatus,
  apiDownloadPendingStudentPdf,
  apiGetPendingStudents,
  apiShowPendingStudent,
} from '@/services/api/admin-pending-students'

export const adminPendingStudentsKeys = {
  all: ['admin', 'pending-students'],
  list: (filters, pagination) => ['admin', 'pending-students', 'list', filters, pagination],
  detail: (id) => ['admin', 'pending-students', 'detail', id],
}

export const usePendingStudentsListQuery = (filtersRef, paginationRef, options = {}) =>
  useQuery({
    queryKey: ['admin', 'pending-students', 'list', filtersRef, paginationRef],
    queryFn: () =>
      apiGetPendingStudents({ ...cleanFilters(filtersRef.value), ...paginationRef.value }),
    ...options,
  })

export const usePendingStudentQuery = (idRef, options = {}) =>
  useQuery({
    queryKey: ['admin', 'pending-students', 'detail', idRef],
    queryFn: () => apiShowPendingStudent(idRef.value),
    select: (response) => response?.data ?? response,
    ...options,
  })

export const useChangePendingStudentStatusMutation = () =>
  useMutation({
    mutationFn: ({ id, payload }) => apiChangePendingStudentStatus(id, payload),
  })

export const useDownloadPendingStudentPdfMutation = () =>
  useMutation({ mutationFn: (id) => apiDownloadPendingStudentPdf(id) })
