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

import type { EventType } from '@/client/models/EventType';
import { ReposService } from '@/client/services/ReposService';

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

  return useMutation({
    mutationFn: (params: { url: string; events: EventType[] }) =>
      ReposService.repoCreateWebhook({ workspaceName, repoName, requestBody: params }),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ['repo', workspaceName, repoName, 'webhooks'] });
    },
  });
}
