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

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

export function useCreateReview() {
  const { workspaceName = '', repoName = '' } = useParams();
  const qc = useQueryClient();

  return useMutation({
    mutationFn: ({ number, ...params }: CreateReviewParams & { number: number }) =>
      PullRequestsService.prCreateReview({
        workspaceName,
        repoName,
        number,
        requestBody: params,
      }),
    onSuccess: (_, vars) => {
      qc.invalidateQueries({
        queryKey: ['workspace', workspaceName, 'repos', repoName, 'prs', vars.number],
      });
    },
  });
}
