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

export function useWorkLogList(projectId: string | undefined) {
  return useQuery({
    queryKey: queryKeys.workLogs.list(projectId!),
    queryFn: () => listWorkLogs(projectId!),
    enabled: !!projectId,
  });
}

export function useCreateWorkLog(projectId: string) {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (data: {
      task_id: number;
      summary: string;
      files_changed?: string;
      test_result?: string;
    }) => createWorkLog(projectId, data),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: queryKeys.workLogs.list(projectId) });
      qc.invalidateQueries({ queryKey: queryKeys.projects.stats(projectId) });
      playSound("google-dokey");
    },
    onError: () => {
      playSound("hmm");
    },
  });
}
