import { keepPreviousData, useQuery } from '@tanstack/react-query';
import { notificationsApi } from '../apis';
import { notificationKeys } from '../keys';
import { NOTIFICATIONS_GC_TIME, NOTIFICATIONS_LIST_STALE_TIME, NOTIFICATIONS_PAGE_SIZE } from '../constants';

/**
 * The notification center list — **unread-first** (server ordering). `limit` grows for "load more" (page 1,
 * a larger page) so the visible list accumulates without a fragile page-by-page merge (unread-first ordering
 * shifts as rows are read). The limit keys the cache, so growing it caches independently and `keepPreviousData`
 * avoids a flash. **Not polled** — only `useUnreadCount` revalidates on an interval; opening a notification /
 * mark-all `setQueryData`s this cache and invalidates on settle.
 */
export function useNotifications(limit: number = NOTIFICATIONS_PAGE_SIZE) {
  const params = { page: 1, pageSize: limit };
  return useQuery({
    queryKey: notificationKeys.list(params),
    queryFn: () => notificationsApi.listNotifications(params),
    placeholderData: keepPreviousData,
    staleTime: NOTIFICATIONS_LIST_STALE_TIME,
    gcTime: NOTIFICATIONS_GC_TIME,
  });
}
