import { useMutation } from '@tanstack/react-query';

import { persistAuthTokens } from '@/lib/auth/session';

import { authApi } from '../apis';

/**
 * Explicitly rotate the token pair (e.g. after selecting a role, whose claim lives inside the
 * access token). The **automatic** silent refresh on an expired access token is owned by the
 * fetch layer (`@/lib/api/refresh`); this hook is the on-demand path and shares the same
 * `persistAuthTokens` writer so token storage never diverges.
 */
export function useRefresh() {
  return useMutation({
    mutationFn: (refreshToken: string) => authApi.refresh({ refreshToken }),
    onSuccess: (tokens) => persistAuthTokens(tokens),
  });
}
