import { useQuery } from '@tanstack/vue-query'
import { cleanFilters } from '@/utils/clean-filters'
import { apiGetStudentHomeworks, apiShowStudentHomework } from '@/services/api/student-homeworks'

export const studentHomeworksKeys = {
  list: (filters, pagination) => ['student', 'homeworks', 'list', filters, pagination],
  detail: (id) => ['student', 'homeworks', 'detail', id],
}

export const useStudentHomeworksListQuery = (filtersRef, paginationRef, options = {}) =>
  useQuery({
    queryKey: ['student', 'homeworks', 'list', filtersRef, paginationRef],
    queryFn: () =>
      apiGetStudentHomeworks({
        ...cleanFilters(filtersRef?.value || {}),
        ...paginationRef?.value,
      }),
    select: (response) => ({
      data: response?.data?.items ?? response?.data ?? [],
      meta: response?.data?.meta ?? response?.meta,
    }),
    ...options,
  })

export const useStudentHomeworkQuery = (idRef, options = {}) =>
  useQuery({
    queryKey: ['student', 'homeworks', 'detail', idRef],
    queryFn: () => apiShowStudentHomework(idRef.value),
    select: (response) => response?.data ?? response,
    ...options,
  })
