import { cleanFilters } from '@/utils/clean-filters'
import { useMutation, useQuery } from '@tanstack/vue-query'
import {
  apiAddAdminTerm,
  apiAddAdminTermCourses,
  apiAddAdminTermStudents,
  apiCloneAdminTerm,
  apiDeleteAdminTerm,
  apiGetAdminTerms,
  apiGetAdminTermCourses,
  apiGetAdminTermEnrollees,
  apiGetAdminTermStudents,
  apiRemoveAdminTermCourse,
  apiRemoveAdminTermStudent,
  apiShowAdminTerm,
  apiToggleAdminTermStudentLeave,
  apiUpdateAdminTerm,
} from '@/services/api/admin-terms'

export const adminTermsKeys = {
  all: ['admin', 'terms'],
  list: (filters, pagination) => ['admin', 'terms', 'list', filters, pagination],
  detail: (id) => ['admin', 'terms', 'detail', id],
  students: (termId, filters, pagination) => [
    'admin',
    'terms',
    'students',
    termId,
    filters,
    pagination,
  ],
  enrollees: (termId, filters, pagination) => [
    'admin',
    'terms',
    'enrollees',
    termId,
    filters,
    pagination,
  ],
  courses: (termId, filters, pagination) => [
    'admin',
    'terms',
    'courses',
    termId,
    filters,
    pagination,
  ],
}

export const useAdminTermsListQuery = (filtersRef, paginationRef, options = {}) =>
  useQuery({
    queryKey: ['admin', 'terms', 'list', filtersRef, paginationRef],
    queryFn: () => apiGetAdminTerms({ ...cleanFilters(filtersRef.value), ...paginationRef.value }),
    select: (response) => ({
      data: response?.data?.items ?? response?.data ?? [],
      meta: response?.data?.meta ?? response?.meta,
    }),
    ...options,
  })

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

export const useAddAdminTermMutation = () =>
  useMutation({ mutationFn: (payload) => apiAddAdminTerm(payload) })

export const useUpdateAdminTermMutation = () =>
  useMutation({ mutationFn: ({ id, payload }) => apiUpdateAdminTerm(id, payload) })

export const useDeleteAdminTermMutation = () =>
  useMutation({ mutationFn: (id) => apiDeleteAdminTerm(id) })

export const useCloneAdminTermMutation = () =>
  useMutation({ mutationFn: (id) => apiCloneAdminTerm(id) })

export const useAdminTermStudentsQuery = (termIdRef, filtersRef, paginationRef, options = {}) =>
  useQuery({
    queryKey: ['admin', 'terms', 'students', termIdRef, filtersRef, paginationRef],
    queryFn: () =>
      apiGetAdminTermStudents(termIdRef.value, {
        ...cleanFilters(filtersRef.value),
        ...paginationRef.value,
      }),
    ...options,
  })

export const useAdminTermEnrolleesQuery = (termIdRef, filtersRef, paginationRef, options = {}) =>
  useQuery({
    queryKey: ['admin', 'terms', 'enrollees', termIdRef, filtersRef, paginationRef],
    queryFn: () =>
      apiGetAdminTermEnrollees(termIdRef.value, {
        ...cleanFilters(filtersRef?.value || {}),
        ...paginationRef?.value,
      }),
    select: (response) => ({
      data: response?.data?.items ?? response?.data ?? [],
      meta: response?.data?.meta ?? response?.meta,
    }),
    ...options,
  })

export const useAddAdminTermStudentsMutation = () =>
  useMutation({
    mutationFn: ({ termId, payload }) => apiAddAdminTermStudents(termId, payload),
  })

export const useRemoveAdminTermStudentMutation = () =>
  useMutation({
    mutationFn: ({ termId, userId }) => apiRemoveAdminTermStudent(termId, userId),
  })

export const useToggleAdminTermStudentLeaveMutation = () =>
  useMutation({
    mutationFn: ({ termId, userId, payload }) =>
      apiToggleAdminTermStudentLeave(termId, userId, payload),
  })

export const useAdminTermCoursesQuery = (termIdRef, filtersRef, paginationRef, options = {}) =>
  useQuery({
    queryKey: ['admin', 'terms', 'courses', termIdRef, filtersRef, paginationRef],
    queryFn: () =>
      apiGetAdminTermCourses(termIdRef.value, {
        ...cleanFilters(filtersRef?.value || {}),
        ...paginationRef?.value,
      }),
    ...options,
  })

export const useAddAdminTermCoursesMutation = () =>
  useMutation({
    mutationFn: ({ termId, payload }) => apiAddAdminTermCourses(termId, payload),
  })

export const useRemoveAdminTermCourseMutation = () =>
  useMutation({
    mutationFn: ({ termId, courseId }) => apiRemoveAdminTermCourse(termId, courseId),
  })
