import { keepPreviousData, useQuery } from '@tanstack/react-query';
import { reviewsApi } from '../apis';
import { reviewKeys } from '../keys';
import { MODERATION_QUEUE_STALE_TIME, REVIEWS_GC_TIME, REVIEWS_PAGE_SIZE } from '../constants';
import type { ModerationQueueFilters } from '../types';

/**
 * The admin moderation worklist — reviews awaiting a decision, filtered by `status` (default
 * `pending_moderation`) + paginated. **Admin-scoped**: these rows carry moderation internals (`moderationReason`,
 * `lowRatingAlertId`) and are never rendered on a customer/nurse surface. Filters + page key the cache so
 * switching a status tab or paging never refetches a page already held; `keepPreviousData` avoids an empty flash
 * while the next page/tab loads. A `useModerateReview` mutation invalidates this queue on success.
 */
export function useModerationQueue(filters: ModerationQueueFilters, page = 1) {
  return useQuery({
    queryKey: reviewKeys.moderationQueue(filters, { page, pageSize: REVIEWS_PAGE_SIZE }),
    queryFn: () => reviewsApi.listModerationQueue(filters, { page, pageSize: REVIEWS_PAGE_SIZE }),
    staleTime: MODERATION_QUEUE_STALE_TIME,
    gcTime: REVIEWS_GC_TIME,
    placeholderData: keepPreviousData,
  });
}
