import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { queryKeys } from "./keys";
import { scoreReleaseService } from "@/services/score-release.service";

export function usePendingScores(params?: { page?: number; limit?: number }) {
  return useQuery({
    queryKey: queryKeys.scoreRelease.pending(params as Record<string, unknown> | undefined),
    queryFn: () => scoreReleaseService.getPending(params),
  });
}

export function useReleaseScore() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (attemptId: number) => scoreReleaseService.release(attemptId),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ["score-release", "pending"] });
    },
  });
}

export function useRejectScore() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({ attemptId, reason }: { attemptId: number; reason: string }) =>
      scoreReleaseService.reject(attemptId, reason),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ["score-release", "pending"] });
    },
  });
}

export function useBulkRelease() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (attemptIds: number[]) => scoreReleaseService.bulkRelease(attemptIds),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ["score-release", "pending"] });
    },
  });
}
