import { useQuery, useMutation } from "@tanstack/react-query";
import { queryKeys } from "./keys";
import { examSessionService } from "@/services/exam-session.service";
import type { ExamAutoSave } from "@/types";

export function useExamSession(examId: number) {
  return useQuery({
    queryKey: queryKeys.examSession.session(examId),
    queryFn: () => examSessionService.getSession(examId),
    enabled: !!examId,
  });
}

export function useExamStatus(examId: number, enabled: boolean) {
  return useQuery({
    queryKey: queryKeys.examSession.status(examId),
    queryFn: () => examSessionService.getStatus(examId),
    enabled: !!examId && enabled,
    refetchInterval: 10000,
  });
}

export function useExamAutoSave() {
  return useMutation({
    mutationFn: (data: { examId: number; payload: ExamAutoSave }) =>
      examSessionService.autoSave(data.examId, data.payload),
  });
}

export function useExamSubmit() {
  return useMutation({
    mutationFn: (examId: number) => examSessionService.submit(examId),
  });
}
