import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { queryKeys } from "./keys";
import { assignmentsService, type AssignmentListParams } from "@/services/assignments.service";
import type { AssignmentCreateRequest } from "@/types";

export function useAssignments(params?: AssignmentListParams) {
  return useQuery({
    queryKey: queryKeys.assignments.list(params),
    queryFn: () => assignmentsService.list(params),
  });
}

export function useAssignment(id: number) {
  return useQuery({
    queryKey: queryKeys.assignments.detail(id),
    queryFn: () => assignmentsService.getById(id),
    enabled: !!id,
  });
}

export function useCreateAssignment() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (data: AssignmentCreateRequest) => assignmentsService.create(data),
    onSuccess: () => qc.invalidateQueries({ queryKey: ["assignments"] }),
  });
}
