import { useQuery } from '@tanstack/react-query';
import { geographyApi } from '../apis';
import { geographyKeys } from '../keys';
import { GEO_GC_TIME, GEO_STALE_TIME } from '../constants';

/**
 * The active districts of a city (ordered). Enabled only once a city is selected. An **empty
 * list is valid** — a whole-city-only city (e.g. Mashhad) has no districts, which the cascade
 * surfaces as the "whole city" affordance rather than an error.
 */
export function useDistricts(cityId?: number | null) {
  return useQuery({
    queryKey: geographyKeys.districts(cityId),
    queryFn: () => geographyApi.listDistricts(cityId as number),
    enabled: cityId != null,
    staleTime: GEO_STALE_TIME,
    gcTime: GEO_GC_TIME,
  });
}
