import { useQuery } from '@tanstack/react-query';

import { GitService } from '@/client/services/GitService';

export function useGitDiffStats(
  workspaceName: string | null,
  repoName: string | null,
  base: string | null,
  head: string | null,
) {
  return useQuery({
    queryKey: ['repo', workspaceName, repoName, 'git', 'diff-stats', { base, head }],
    queryFn: () => {
      if (!workspaceName || !repoName || !base || !head) throw new Error('Missing params');
      return GitService.gitDiffStats({ workspaceName, repoName, base, head }).then(
        (r) => r.data,
      );
    },
    enabled: !!workspaceName && !!repoName && !!base && !!head,
    staleTime: 1000 * 60 * 5,
  });
}
