import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { queryKeys } from "./keys";
import { gradingService } from "@/services/grading.service";
import type { GradeSubmission } from "@/types";

export function useGradingQueue(params?: { skill?: string; status?: string; exam_id?: number }) {
  return useQuery({
    queryKey: queryKeys.grading.queue(params),
    queryFn: () => gradingService.getQueue(params),
  });
}

export function useStudentResponse(attemptId: number, skill: string) {
  return useQuery({
    queryKey: queryKeys.grading.response(attemptId, skill),
    queryFn: () => gradingService.getStudentResponse(attemptId, skill),
    enabled: !!attemptId,
  });
}

export function useGradingRubric(attemptId: number, skill: string) {
  return useQuery({
    queryKey: queryKeys.grading.rubric(attemptId, skill),
    queryFn: () => gradingService.getRubric(attemptId, skill),
    enabled: !!attemptId,
  });
}

export function useAIGradeSuggestion() {
  return useMutation({
    mutationFn: (data: { attemptId: number; skill: string }) =>
      gradingService.getAISuggestion(data.attemptId, data.skill),
  });
}

export function useSubmitGrade() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (data: GradeSubmission) => gradingService.submitGrade(data),
    onSuccess: () => qc.invalidateQueries({ queryKey: ["grading"] }),
  });
}
