import { IProjectsQueryParams, projectsService } from "@/api/services/project.service";
import { T_projects } from "@/types/projects.type";
import { queryKeys } from "@/utils/queryKeys";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";


export const useProjects = (params?: IProjectsQueryParams) => {
  return useQuery({
    queryKey: [...queryKeys.projects, params],
    queryFn: () => projectsService.getProjects(params),
  });
};

export const useUpdateProject = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({ id, data }: { id: string; data: Partial<T_projects> }) =>
      projectsService.updateProject(id, data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: queryKeys.projects });
    },
  });
};

export const useDeleteProject = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (id: string) => projectsService.deleteProject(id),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: queryKeys.projects });
    },
  });
};

export const useProjectById = (id: string) => {
  return useQuery({
    queryKey: queryKeys.project(id),
    queryFn: () => projectsService.getProjectById(id),
    enabled: !!id,
  });
};