import { useQuery, useMutation } from "@tanstack/react-query";
import { queryKeys } from "./keys";
import { placementService } from "@/services/placement.service";
import type { PlacementSubject, CATAnswer, CATAutoSave } from "@/types";

export function usePlacementStart() {
  return useMutation({
    mutationFn: (subject: PlacementSubject) => placementService.start(subject),
  });
}

export function usePlacementAnswer() {
  return useMutation({
    mutationFn: (data: CATAnswer) => placementService.submitAnswer(data),
  });
}

export function usePlacementAutoSave() {
  return useMutation({
    mutationFn: (data: CATAutoSave) => placementService.autoSave(data),
  });
}

export function usePlacementResults(sessionId: string) {
  return useQuery({
    queryKey: queryKeys.placement.results(sessionId),
    queryFn: () => placementService.getResults(sessionId),
    enabled: !!sessionId,
  });
}

export function useSpeakingStatus(sessionId: string, enabled: boolean) {
  return useQuery({
    queryKey: queryKeys.placement.speakingStatus(sessionId),
    queryFn: () => placementService.getSpeakingStatus(sessionId),
    enabled: !!sessionId && enabled,
    refetchInterval: 30000,
  });
}

export function useLearningPath(sessionId: string) {
  return useQuery({
    queryKey: queryKeys.placement.learningPath(sessionId),
    queryFn: () => placementService.getLearningPath(sessionId),
    enabled: !!sessionId,
  });
}

export function useSpeakingUpload() {
  return useMutation({
    mutationFn: (data: { sessionId: string; promptId: number; audioFile: File }) =>
      placementService.uploadSpeaking(data.sessionId, data.promptId, data.audioFile),
  });
}
