import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { listProjects, createProject, getProjectStats } from "../api";
import { queryKeys } from "../queryKeys";
import { playSound } from "../usePeonSounds";

export function useProjectList() {
  return useQuery({
    queryKey: queryKeys.projects.all,
    queryFn: listProjects,
  });
}

export function useProjectStats(projectId: string | undefined) {
  return useQuery({
    queryKey: queryKeys.projects.stats(projectId!),
    queryFn: () => getProjectStats(projectId!),
    enabled: !!projectId,
  });
}

export function useCreateProject() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({ id, path }: { id: string; path: string }) =>
      createProject(id, path),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: queryKeys.projects.all });
      playSound("be-happy-to");
    },
    onError: () => {
      playSound("hmm");
    },
  });
}
