// src/config/queryKeys.ts
/**
 * Centralized query keys for TanStack Query.
 *
 * This file provides a single source of truth for all query keys used
 * throughout the application. Using these factory functions ensures
 * consistent key naming and proper cache invalidation.
 *
 * @example
 * ```tsx
 * // In a query hook
 * useQuery({
 *   queryKey: queryKeys.flyers(10, 0),
 *   queryFn: fetchFlyers,
 * });
 *
 * // For cache invalidation
 * queryClient.invalidateQueries({ queryKey: queryKeys.watchedItems() });
 * ```
 */
export const queryKeys = {
  // User Features
  flyers: (limit: number, offset: number) => ['flyers', { limit, offset }] as const,
  flyerItems: (flyerId: number) => ['flyer-items', flyerId] as const,
  flyerItemsBatch: (flyerIds: number[]) =>
    ['flyer-items-batch', flyerIds.sort().join(',')] as const,
  flyerItemsCount: (flyerIds: number[]) =>
    ['flyer-items-count', flyerIds.sort().join(',')] as const,
  masterItems: () => ['master-items'] as const,
  watchedItems: () => ['watched-items'] as const,
  shoppingLists: () => ['shopping-lists'] as const,

  // Auth & Profile
  authProfile: () => ['auth-profile'] as const,
  userAddress: (addressId: number | null) => ['user-address', addressId] as const,
  userProfileData: () => ['user-profile-data'] as const,

  // Admin Features
  activityLog: (limit: number, offset: number) => ['activity-log', { limit, offset }] as const,
  applicationStats: () => ['application-stats'] as const,
  suggestedCorrections: () => ['suggested-corrections'] as const,
  categories: () => ['categories'] as const,

  // Analytics
  bestSalePrices: () => ['best-sale-prices'] as const,
  priceHistory: (masterItemIds: number[]) =>
    ['price-history', [...masterItemIds].sort((a, b) => a - b).join(',')] as const,
  leaderboard: (limit: number) => ['leaderboard', limit] as const,
} as const;

/**
 * Base keys for partial matching in cache invalidation.
 *
 * Use these when you need to invalidate all queries of a certain type
 * regardless of their parameters.
 *
 * @example
 * ```tsx
 * // Invalidate all flyer-related queries
 * queryClient.invalidateQueries({ queryKey: queryKeyBases.flyers });
 * ```
 */
export const queryKeyBases = {
  flyers: ['flyers'] as const,
  flyerItems: ['flyer-items'] as const,
  flyerItemsBatch: ['flyer-items-batch'] as const,
  flyerItemsCount: ['flyer-items-count'] as const,
  masterItems: ['master-items'] as const,
  watchedItems: ['watched-items'] as const,
  shoppingLists: ['shopping-lists'] as const,
  authProfile: ['auth-profile'] as const,
  userAddress: ['user-address'] as const,
  userProfileData: ['user-profile-data'] as const,
  activityLog: ['activity-log'] as const,
  applicationStats: ['application-stats'] as const,
  suggestedCorrections: ['suggested-corrections'] as const,
  categories: ['categories'] as const,
  bestSalePrices: ['best-sale-prices'] as const,
  priceHistory: ['price-history'] as const,
  leaderboard: ['leaderboard'] as const,
} as const;

export type QueryKeys = typeof queryKeys;
export type QueryKeyBases = typeof queryKeyBases;
