import { useMutation, useQueryClient } from '@tanstack/react-query';
import { adminApi } from '../apis';
import { adminKeys } from '../keys';
import type { AdminRole } from '../types';

/** Revoke an admin role from a user (sets `revokedAt`). Invalidate the roles list. */
export function useRevokeRole() {
  const queryClient = useQueryClient();
  return useMutation<void, unknown, { userId: number; role: AdminRole }>({
    mutationFn: ({ userId, role }) => adminApi.revokeRole(userId, role),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: adminKeys.roles() });
    },
  });
}
