import { useMutation, useQueryClient } from "@tanstack/react-query";
import * as api from "../service/TaskPhaseService";
import { CreateTaskPhaseType, UpdateTaskPhaseType } from "../types/TaskPhaseTypes";

export const useCreateTaskPhase = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (variables: CreateTaskPhaseType) => api.createTaskPhase(variables),
    onSuccess: (_data, variables) => {
      queryClient.invalidateQueries({
        queryKey: ["task-phases", variables.projectId],
      });
      queryClient.invalidateQueries({ queryKey: ["project", variables.projectId] });
    },
  });
};

export const useUpdateTaskPhase = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ id, params }: { id: string; params: UpdateTaskPhaseType; projectId: string }) =>
      api.updateTaskPhase(id, params),
    onSuccess: (_data, variables) => {
      queryClient.invalidateQueries({ queryKey: ["project", variables.projectId] });
    },
  });
};

export const useDeleteTaskPhase = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ id }: { id: string; projectId: string }) => api.deleteTaskPhase(id),
    onSuccess: (_data, variables) => {
      queryClient.invalidateQueries({ queryKey: ["project", variables.projectId] });
    },
  });
};
