import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { queryKeys } from "./keys";
import { coursewareService } from "@/services/courseware.service";
import type {
  ChapterCreateRequest,
  WorkbenchGenerateRequest,
} from "@/types/courseware";

export function useChapters(courseId: number) {
  return useQuery({
    queryKey: queryKeys.chapters(courseId),
    queryFn: () => coursewareService.listChapters(courseId),
    enabled: !!courseId,
  });
}

export function useChapter(id: number) {
  return useQuery({
    queryKey: queryKeys.chapter(id),
    queryFn: () => coursewareService.getChapter(id),
    enabled: !!id,
  });
}

export function useChapterMaterials(chapterId: number) {
  return useQuery({
    queryKey: queryKeys.chapterMaterials(chapterId),
    queryFn: () => coursewareService.listMaterials(chapterId),
    enabled: !!chapterId,
  });
}

export function useChapterProgress(chapterId: number) {
  return useQuery({
    queryKey: queryKeys.chapterProgress(chapterId),
    queryFn: () => coursewareService.getChapterProgress(chapterId),
    enabled: !!chapterId,
  });
}

export function useCreateChapter() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({ courseId, data }: { courseId: number; data: ChapterCreateRequest }) =>
      coursewareService.createChapter(courseId, data),
    onSuccess: (_, { courseId }) => {
      qc.invalidateQueries({ queryKey: queryKeys.chapters(courseId) });
    },
  });
}

export function useUpdateChapter() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({ id, data }: { id: number; data: Partial<ChapterCreateRequest> }) =>
      coursewareService.updateChapter(id, data),
    onSuccess: (chapter) => {
      qc.invalidateQueries({ queryKey: queryKeys.chapter(chapter.id) });
      qc.invalidateQueries({ queryKey: queryKeys.chapters(chapter.course_id) });
    },
  });
}

export function useDeleteChapter() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({ id, courseId }: { id: number; courseId: number }) =>
      coursewareService.deleteChapter(id),
    onSuccess: (_, { courseId }) => {
      qc.invalidateQueries({ queryKey: queryKeys.chapters(courseId) });
    },
  });
}

export function useUnlockChapter() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({ id, courseId }: { id: number; courseId: number }) =>
      coursewareService.unlockChapter(id),
    onSuccess: (_, { id, courseId }) => {
      qc.invalidateQueries({ queryKey: queryKeys.chapter(id) });
      qc.invalidateQueries({ queryKey: queryKeys.chapters(courseId) });
    },
  });
}

export function useLockChapter() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({ id, courseId }: { id: number; courseId: number }) =>
      coursewareService.lockChapter(id),
    onSuccess: (_, { id, courseId }) => {
      qc.invalidateQueries({ queryKey: queryKeys.chapter(id) });
      qc.invalidateQueries({ queryKey: queryKeys.chapters(courseId) });
    },
  });
}

export function useReorderChapters() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({ courseId, body }: { courseId: number; body: { ids: number[] } }) =>
      coursewareService.reorderChapters(courseId, body),
    onSuccess: (_, { courseId }) => {
      qc.invalidateQueries({ queryKey: queryKeys.chapters(courseId) });
    },
  });
}

export function useUploadMaterial() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({ chapterId, formData }: { chapterId: number; formData: FormData }) =>
      coursewareService.uploadMaterial(chapterId, formData),
    onSuccess: (_, { chapterId }) => {
      qc.invalidateQueries({ queryKey: queryKeys.chapterMaterials(chapterId) });
      qc.invalidateQueries({ queryKey: queryKeys.chapterProgress(chapterId) });
      qc.invalidateQueries({ queryKey: queryKeys.chapter(chapterId) });
    },
  });
}

export function useDeleteMaterial() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({ id, chapterId }: { id: number; chapterId: number }) =>
      coursewareService.deleteMaterial(id),
    onSuccess: (_, { chapterId }) => {
      qc.invalidateQueries({ queryKey: queryKeys.chapterMaterials(chapterId) });
      qc.invalidateQueries({ queryKey: queryKeys.chapterProgress(chapterId) });
      qc.invalidateQueries({ queryKey: queryKeys.chapter(chapterId) });
    },
  });
}

export function useMarkMaterialViewed() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({ id, chapterId }: { id: number; chapterId: number }) =>
      coursewareService.markMaterialViewed(id),
    onSuccess: (_, { chapterId }) => {
      qc.invalidateQueries({ queryKey: queryKeys.chapterMaterials(chapterId) });
      qc.invalidateQueries({ queryKey: queryKeys.chapterProgress(chapterId) });
    },
  });
}

export function useCompleteChapter() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (chapterId: number) => coursewareService.markChapterComplete(chapterId),
    onSuccess: (_, chapterId) => {
      qc.invalidateQueries({ queryKey: queryKeys.chapterProgress(chapterId) });
      qc.invalidateQueries({ queryKey: queryKeys.chapter(chapterId) });
    },
  });
}

export function useGenerateOutline() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (data: WorkbenchGenerateRequest) => coursewareService.generateOutline(data),
    onSuccess: (_, data) => {
      qc.invalidateQueries({ queryKey: queryKeys.chapters(data.course_id) });
      qc.invalidateQueries({ queryKey: queryKeys.lms.course(data.course_id) });
    },
  });
}

export function useGenerateChapterContent() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (data: WorkbenchGenerateRequest) => coursewareService.generateChapterContent(data),
    onSuccess: (_, data) => {
      qc.invalidateQueries({ queryKey: queryKeys.chapters(data.course_id) });
      qc.invalidateQueries({ queryKey: queryKeys.lms.course(data.course_id) });
    },
  });
}

export function useGenerateRubric() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (data: WorkbenchGenerateRequest) => coursewareService.generateRubric(data),
    onSuccess: (_, data) => {
      qc.invalidateQueries({ queryKey: queryKeys.chapters(data.course_id) });
      qc.invalidateQueries({ queryKey: queryKeys.lms.course(data.course_id) });
    },
  });
}

export function usePublishWorkbench() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (data: Record<string, unknown>) => coursewareService.publishWorkbench(data),
    onSuccess: (_, data) => {
      const courseId = data.course_id;
      if (typeof courseId === "number") {
        qc.invalidateQueries({ queryKey: queryKeys.chapters(courseId) });
        qc.invalidateQueries({ queryKey: queryKeys.lms.course(courseId) });
      }
    },
  });
}
