import { useMutation, useQueryClient } from '@tanstack/react-query';

import type { CreateReactionParams } from '@/client/models/CreateReactionParams';
import { PullRequestsService } from '@/client/services/PullRequestsService';

export function useAddPrReaction(
  workspaceName: string | null,
  repoName: string | null,
  number: number | null,
) {
  const qc = useQueryClient();

  return useMutation({
    mutationFn: (params: CreateReactionParams) => {
      if (!workspaceName || !repoName || number === null) throw new Error('Missing params');
      return PullRequestsService.prAddReaction({
        workspaceName,
        repoName,
        number,
        requestBody: params,
      });
    },
    onSuccess: () => {
      qc.invalidateQueries({
        queryKey: ['workspace', workspaceName, 'repos', repoName, 'prs', number, 'reactions'],
      });
    },
  });
}
