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

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

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

  return useMutation({
    mutationFn: (params: AddDeployKeyParams) =>
      ReposService.repoAddDeployKey({ workspaceName, repoName, requestBody: params }),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ['repo', workspaceName, repoName, 'deploy-keys'] });
    },
  });
}
