import { useQuery } from '@tanstack/react-query';
import { fetchStats } from '../services/api';
import { useEventStore } from '../store/eventStore';
import type { StatsPayload } from '../types/guest';

export function useStats() {
  const isLoaded = useEventStore(s => s.isLoaded);
  const isSessionValid = useEventStore(s => s.isSessionValid);

  return useQuery<StatsPayload>({
    queryKey: ['stats'],
    queryFn: fetchStats,
    enabled: isLoaded && isSessionValid(),
    refetchInterval: 20_000,
    staleTime: 15_000,
    retry: 2,
  });
}
