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

import { ReposService } from '@/client/services/ReposService';

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

  return useMutation({
    mutationFn: (newOwnerId: string) =>
      ReposService.repoTransferOwner({
        workspaceName,
        repoName,
        requestBody: { new_owner_id: newOwnerId },
      }),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ['repo', workspaceName, repoName] });
    },
  });
}
