import type { PageParams } from '@/lib/api/types';

/**
 * React Query key factory for the catalog domain. Reference data (categories, a category's option
 * groups) is keyed so each is fetched **once** per session and served from cache across the Home
 * grid and the nurse builder. The nurse's own variant list invalidates on every mutation via the
 * `myVariantsLists()` prefix; a single edited variant is refreshed by `variant(id)`.
 */
export const catalogKeys = {
  all: ['catalog'] as const,

  // Reference data — cached for the whole session, never invalidated by this phase.
  categories: () => [...catalogKeys.all, 'categories'] as const,
  categoryOptionGroups: (categoryId?: number | null) =>
    [...catalogKeys.all, 'option-groups', categoryId ?? null] as const,

  // The nurse's offerings — mutable; mutations invalidate the `myVariantsLists()` prefix.
  variants: () => [...catalogKeys.all, 'variants'] as const,
  myVariantsLists: () => [...catalogKeys.variants(), 'mine'] as const,
  myVariants: (params?: PageParams) => [...catalogKeys.myVariantsLists(), params ?? {}] as const,
  variant: (id: number) => [...catalogKeys.variants(), 'detail', id] as const,
};
