import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { queryKeys } from "./keys";
import { academicService } from "@/services/academic.service";
import type { PaginationParams } from "@/types";
import type {
  AcademicYearCreateRequest,
  AcademicTermCreateRequest,
  DepartmentCreateRequest,
} from "@/types/academic";

export function useAcademicYears(params?: PaginationParams) {
  return useQuery({
    queryKey: queryKeys.academicYears.list(params),
    queryFn: () => academicService.listAcademicYears(params),
  });
}

export function useAcademicYear(id: number) {
  return useQuery({
    queryKey: queryKeys.academicYears.detail(id),
    queryFn: () => academicService.getAcademicYear(id),
    enabled: !!id,
  });
}

export function useAcademicTerms(yearId?: number) {
  return useQuery({
    queryKey: queryKeys.academicTerms.list({ year_id: yearId }),
    queryFn: () => academicService.listTerms({ year_id: yearId }),
  });
}

export function useDepartments(params?: PaginationParams) {
  return useQuery({
    queryKey: queryKeys.departments.list(params),
    queryFn: () => academicService.listDepartments(params),
  });
}

export function useCreateAcademicYear() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (data: AcademicYearCreateRequest) => academicService.createAcademicYear(data),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.academicYears.all }),
  });
}

export function useUpdateAcademicYear() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({ id, data }: { id: number; data: Partial<AcademicYearCreateRequest> }) =>
      academicService.updateAcademicYear(id, data),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.academicYears.all }),
  });
}

export function useDeleteAcademicYear() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (id: number) => academicService.deleteAcademicYear(id),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.academicYears.all }),
  });
}

export function useGenerateTerms() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (yearId: number) => academicService.generateTerms(yearId),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.academicTerms.all }),
  });
}

export function useCreateAcademicTerm() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (data: AcademicTermCreateRequest) => academicService.createTerm(data),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.academicTerms.all }),
  });
}

export function useCreateDepartment() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (data: DepartmentCreateRequest) => academicService.createDepartment(data),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.departments.all }),
  });
}

export function useUpdateDepartment() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({ id, data }: { id: number; data: Partial<DepartmentCreateRequest> }) =>
      academicService.updateDepartment(id, data),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.departments.all }),
  });
}

export function useDeleteDepartment() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (id: number) => academicService.deleteDepartment(id),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.departments.all }),
  });
}
