import { useQuery } from '@tanstack/vue-query'
import { cleanFilters } from '@/utils/clean-filters'
import { apiGetStudentTerms, apiShowStudentTerm } from '@/services/api/student-terms'

export const studentTermsKeys = {
  all: ['student', 'terms'],
  list: (filters, pagination) => ['student', 'terms', 'list', filters, pagination],
  detail: (id) => ['student', 'terms', 'detail', id],
}

export const useStudentTermsListQuery = (filtersRef, paginationRef, options = {}) =>
  useQuery({
    queryKey: ['student', 'terms', 'list', filtersRef, paginationRef],
    queryFn: () =>
      apiGetStudentTerms({
        ...cleanFilters(filtersRef.value),
        ...paginationRef.value,
      }),
    select: (response) => {
      const payload = response?.data ?? response ?? {}
      return {
        data: Array.isArray(payload.items) ? payload.items : payload ?? [],
        meta: payload.meta,
      }
    },
    ...options,
  })

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