import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { queryKeys } from "./keys";
import { aiCourseService } from "@/services/ai-course.service";
import type { ExaminerReview } from "@/types";

export function useAiCourse(courseId: number | undefined) {
  return useQuery({
    queryKey: queryKeys.aiCourse.course(courseId ?? 0),
    queryFn: () => aiCourseService.getCourse(courseId!),
    enabled: !!courseId && courseId > 0,
  });
}

export function useAiCourseTracks(courseId: number | undefined) {
  return useQuery({
    queryKey: queryKeys.aiCourse.tracks(courseId ?? 0),
    queryFn: () => aiCourseService.getTracks(courseId!),
    enabled: !!courseId && courseId > 0,
  });
}

export function useCreateEnglishCourse() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (data: { current_level: string; target_level: string; learning_style: string[] }) =>
      aiCourseService.createEnglish(data),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ["ai-course"] });
    },
  });
}

export function useCreateIeltsCourse() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (data: { exam_type: string; target_band: number; skills: string[] }) =>
      aiCourseService.createIelts(data),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ["ai-course"] });
    },
  });
}

export function useQualityGate(courseId: number | undefined) {
  return useQuery({
    queryKey: queryKeys.aiCourse.quality(courseId ?? 0),
    queryFn: () => aiCourseService.getQualityGate(courseId!),
    enabled: !!courseId && courseId > 0,
  });
}

export function useApproveQuality() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (courseId: number) => aiCourseService.approveQuality(courseId),
    onSuccess: (_d, courseId) => {
      qc.invalidateQueries({ queryKey: queryKeys.aiCourse.quality(courseId) });
      qc.invalidateQueries({ queryKey: queryKeys.aiCourse.course(courseId) });
    },
  });
}

export function useRejectQuality() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({ courseId, notes }: { courseId: number; notes: string }) =>
      aiCourseService.rejectQuality(courseId, notes),
    onSuccess: (_d, { courseId }) => {
      qc.invalidateQueries({ queryKey: queryKeys.aiCourse.quality(courseId) });
    },
  });
}

export function useEnglishTaxonomy() {
  return useQuery({
    queryKey: queryKeys.aiCourse.englishTaxonomy,
    queryFn: () => aiCourseService.getEnglishTaxonomy(),
  });
}

export function useIeltsValidation(courseId: number | undefined) {
  return useQuery({
    queryKey: queryKeys.aiCourse.ieltsValidation(courseId ?? 0),
    queryFn: () => aiCourseService.getIeltsValidation(courseId!),
    enabled: !!courseId && courseId > 0,
  });
}

export function useSubmitExaminerReview() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (data: ExaminerReview) => aiCourseService.submitExaminerReview(data),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ["ai-course"] });
    },
  });
}
