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

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

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

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