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

import { useIsAuthenticated } from '@/hooks';

import { authApi } from '../apis';
import { AUTH_ME_STALE_TIME } from '../constants';
import { authKeys } from '../keys';

/**
 * The signed-in user's identity, roles, and onboarding state — the role router's data source and
 * the shells' role source. Only runs once authenticated (no `/me` call for a logged-out visitor).
 */
export function useMe() {
  const isAuthenticated = useIsAuthenticated();
  return useQuery({
    queryKey: authKeys.me(),
    queryFn: () => authApi.getMe(),
    enabled: isAuthenticated,
    staleTime: AUTH_ME_STALE_TIME,
  });
}
