import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { queryKeys } from "./keys";
import { lessonService } from "@/services/lesson.service";
import type { PaginationParams } from "@/types";
import type { LessonCreateRequest } from "@/types/lesson";

export function useLessons(params?: PaginationParams) {
  return useQuery({
    queryKey: queryKeys.lessons.list(params),
    queryFn: () => lessonService.listLessons(params),
  });
}

export function useCreateLesson() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (data: LessonCreateRequest) => lessonService.createLesson(data),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.lessons.all }),
  });
}

export function useUpdateLesson() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({ id, data }: { id: number; data: Partial<LessonCreateRequest> }) =>
      lessonService.updateLesson(id, data),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.lessons.all }),
  });
}

export function useDeleteLesson() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (id: number) => lessonService.deleteLesson(id),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.lessons.all }),
  });
}
