import { useQuery } from '@tanstack/react-query';
import { useIsAuthenticated } from '@/hooks';
import type { PageParams } from '@/lib/api/types';
import { serviceAreasApi } from '../apis';
import { serviceAreaKeys } from '../keys';
import { SERVICE_AREAS_STALE_TIME } from '../constants';

/**
 * The nurse's coverage areas (whole-city first). A deliberate staleTime keeps them warm across
 * remounts; add/remove invalidate the list. The screen also reads this cache for the fast-path
 * duplicate check before firing an add.
 */
export function useServiceAreas(params?: PageParams) {
  const isAuthenticated = useIsAuthenticated();
  return useQuery({
    queryKey: serviceAreaKeys.list(params),
    queryFn: () => serviceAreasApi.list(params),
    enabled: isAuthenticated,
    staleTime: SERVICE_AREAS_STALE_TIME,
  });
}
