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

/** Grant an admin role to a user (records `grantedBy`/`grantedAt`). Invalidate the roles list. */
export function useGrantRole() {
  const queryClient = useQueryClient();
  return useMutation<void, unknown, { userId: number; role: AdminRole }>({
    mutationFn: ({ userId, role }) => adminApi.grantRole(userId, role),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: adminKeys.roles() });
    },
  });
}
