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

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

export function usePrEvents(
  workspaceName: string | null,
  repoName: string | null,
  number: number | null,
  limit = 50,
  offset = 0,
) {
  return useQuery({
    queryKey: ['workspace', workspaceName, 'repos', repoName, 'prs', number, 'events', { limit, offset }],
    queryFn: () => {
      if (!workspaceName || !repoName || number === null) throw new Error('Missing params');
      return PullRequestsService.prListEvents({
        workspaceName,
        repoName,
        number,
        limit,
        offset,
      }).then((r) => r.data);
    },
    enabled: !!workspaceName && !!repoName && number !== null,
    staleTime: 1000 * 60,
  });
}
