import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { queryKeys } from "./keys";
import { examsService } from "@/services";
import type { ExamModule, PaginationParams } from "@/types";

export function useExams(module: ExamModule, params?: PaginationParams) {
  return useQuery({
    queryKey: queryKeys.exams.list(module, params),
    queryFn: () => examsService.list(module, params),
  });
}

export function useExam(module: ExamModule, id: number) {
  return useQuery({
    queryKey: queryKeys.exams.detail(module, id),
    queryFn: () => examsService.getById(module, id),
    enabled: !!id,
  });
}

export function useRubrics(params?: PaginationParams) {
  return useQuery({
    queryKey: queryKeys.exams.rubrics(params),
    queryFn: () => examsService.listRubrics(params),
  });
}

export function useRubricGroups(params?: PaginationParams) {
  return useQuery({
    queryKey: queryKeys.exams.rubricGroups(params),
    queryFn: () => examsService.listRubricGroups(params),
  });
}

export function useExamStructures(params?: PaginationParams & { entity_id?: number }) {
  return useQuery({
    queryKey: queryKeys.exams.structures(params),
    queryFn: () => examsService.listStructures(params),
  });
}

export function useDeleteExam() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (id: number) => examsService.delete(id),
    onSuccess: () => qc.invalidateQueries({ queryKey: ["exams"] }),
  });
}
