import { cleanFilters } from '@/utils/clean-filters'
import { useMutation, useQuery } from '@tanstack/vue-query'
import {
  apiAddAdminAssignment,
  apiDeleteAdminAssignment,
  apiGetAdminAssignmentSubmissions,
  apiGetAdminAssignments,
  apiReviewAdminAssignmentSubmission,
  apiShowAdminAssignment,
  apiShowAdminAssignmentSubmission,
  apiUpdateAdminAssignment,
} from '@/services/api/admin-assignments'

export const adminAssignmentsKeys = {
  all: ['admin', 'assignments'],
  list: (filters, pagination) => ['admin', 'assignments', 'list', filters, pagination],
  detail: (id) => ['admin', 'assignments', 'detail', id],
  submissions: (assignmentId, filters, pagination) => [
    'admin',
    'assignments',
    'submissions',
    assignmentId,
    filters,
    pagination,
  ],
  submissionDetail: (assignmentId, submissionId) => [
    'admin',
    'assignments',
    'submissions',
    assignmentId,
    'detail',
    submissionId,
  ],
}

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

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

export const useAdminAssignmentSubmissionsQuery = (
  assignmentIdRef,
  filtersRef,
  paginationRef,
  options = {}
) =>
  useQuery({
    queryKey: ['admin', 'assignments', 'submissions', assignmentIdRef, filtersRef, paginationRef],
    queryFn: () =>
      apiGetAdminAssignmentSubmissions(assignmentIdRef.value, {
        ...cleanFilters(filtersRef?.value || {}),
        ...paginationRef?.value,
      }),
    select: (response) => ({
      data: response?.data?.items ?? response?.data ?? [],
      meta: response?.data?.meta ?? response?.meta,
    }),
    ...options,
  })

export const useAdminAssignmentSubmissionQuery = (assignmentIdRef, submissionIdRef, options = {}) =>
  useQuery({
    queryKey: ['admin', 'assignments', 'submissions', assignmentIdRef, 'detail', submissionIdRef],
    queryFn: () => apiShowAdminAssignmentSubmission(assignmentIdRef.value, submissionIdRef.value),
    select: (response) => response?.data ?? response,
    ...options,
  })

export const useAddAdminAssignmentMutation = () =>
  useMutation({ mutationFn: (payload) => apiAddAdminAssignment(payload) })

export const useUpdateAdminAssignmentMutation = () =>
  useMutation({
    mutationFn: ({ id, payload }) => apiUpdateAdminAssignment(id, payload),
  })

export const useDeleteAdminAssignmentMutation = () =>
  useMutation({ mutationFn: (id) => apiDeleteAdminAssignment(id) })

export const useReviewAdminAssignmentSubmissionMutation = () =>
  useMutation({
    mutationFn: ({ assignmentId, submissionId, payload }) =>
      apiReviewAdminAssignmentSubmission(assignmentId, submissionId, payload),
  })
