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

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

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

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