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

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

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

  return useMutation({
    mutationFn: ({ userId, role }: { userId: string; role?: Role }) =>
      ReposService.repoAddMember({
        workspaceName,
        repoName,
        requestBody: { user_id: userId, role },
      }),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ['repo', workspaceName, repoName, 'members'] });
    },
  });
}
