/**
 * React Query (TanStack Query) Client Configuration
 *
 * Centralized query client with optimized defaults for the Tour Builder app.
 * This replaces direct axios calls with cached, deduplicated queries.
 */

import { QueryClient } from '@tanstack/react-query';

/**
 * Default stale time: 5 minutes
 * Data is considered fresh for 5 minutes before background refetching
 */
const DEFAULT_STALE_TIME = 5 * 60 * 1000;

/**
 * Default garbage collection time: 10 minutes
 * Inactive queries are garbage collected after 10 minutes
 */
const DEFAULT_GC_TIME = 10 * 60 * 1000;

/**
 * Singleton query client instance
 */
export const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      // Data is fresh for 5 minutes
      staleTime: DEFAULT_STALE_TIME,
      // Keep inactive data for 10 minutes (was cacheTime in v4)
      gcTime: DEFAULT_GC_TIME,
      // Only retry once on failure
      retry: 1,
      // Don't refetch on window focus (prevents interruption during editing)
      refetchOnWindowFocus: false,
      // Don't refetch on reconnect by default
      refetchOnReconnect: false,
    },
    mutations: {
      // Mutations don't retry by default
      retry: 0,
    },
  },
});

/**
 * Query key factories for consistent query key generation
 * Use these to ensure cache invalidation works correctly
 */
export const queryKeys = {
  // Projects
  projects: {
    all: ['projects'] as const,
    list: (filters?: unknown) =>
      [...queryKeys.projects.all, 'list', filters] as const,
    detail: (id: string) => [...queryKeys.projects.all, 'detail', id] as const,
  },

  // Tour Pages
  tourPages: {
    all: ['tourPages'] as const,
    list: (projectId: string, environment?: string) =>
      [...queryKeys.tourPages.all, 'list', { projectId, environment }] as const,
    detail: (id: string) => [...queryKeys.tourPages.all, 'detail', id] as const,
    byProject: (projectId: string) =>
      [...queryKeys.tourPages.all, 'byProject', projectId] as const,
  },

  // Assets
  assets: {
    all: ['assets'] as const,
    list: (projectId: string, filters?: unknown) =>
      [...queryKeys.assets.all, 'list', { projectId, filters }] as const,
    detail: (id: string) => [...queryKeys.assets.all, 'detail', id] as const,
    byProject: (projectId: string) =>
      [...queryKeys.assets.all, 'byProject', projectId] as const,
  },

  // Element Defaults
  elementDefaults: {
    all: ['elementDefaults'] as const,
    global: () => [...queryKeys.elementDefaults.all, 'global'] as const,
    project: (projectId: string) =>
      [...queryKeys.elementDefaults.all, 'project', projectId] as const,
  },

  // Users
  users: {
    all: ['users'] as const,
    list: (filters?: unknown) =>
      [...queryKeys.users.all, 'list', filters] as const,
    current: () => [...queryKeys.users.all, 'current'] as const,
    detail: (id: string) => [...queryKeys.users.all, 'detail', id] as const,
  },

  // Roles
  roles: {
    all: ['roles'] as const,
    list: (filters?: unknown) =>
      [...queryKeys.roles.all, 'list', filters] as const,
    detail: (id: string) => [...queryKeys.roles.all, 'detail', id] as const,
  },

  // Permissions
  permissions: {
    all: ['permissions'] as const,
    list: () => [...queryKeys.permissions.all, 'list'] as const,
  },

  // Access Logs
  accessLogs: {
    all: ['accessLogs'] as const,
    list: (filters?: unknown) =>
      [...queryKeys.accessLogs.all, 'list', filters] as const,
  },

  // Asset Variants
  assetVariants: {
    all: ['assetVariants'] as const,
    list: (assetId: string) =>
      [...queryKeys.assetVariants.all, 'list', assetId] as const,
  },

  // Project Memberships
  projectMemberships: {
    all: ['projectMemberships'] as const,
    list: (projectId: string) =>
      [...queryKeys.projectMemberships.all, 'list', projectId] as const,
  },

  // Project Audio Tracks
  projectAudioTracks: {
    all: ['projectAudioTracks'] as const,
    list: (projectId: string) =>
      [...queryKeys.projectAudioTracks.all, 'list', projectId] as const,
    detail: (id: string) =>
      [...queryKeys.projectAudioTracks.all, 'detail', id] as const,
  },

  // Publish Events
  publishEvents: {
    all: ['publishEvents'] as const,
    list: (projectId: string) =>
      [...queryKeys.publishEvents.all, 'list', projectId] as const,
  },

  // PWA Caches
  pwaCaches: {
    all: ['pwaCaches'] as const,
    list: (projectId: string) =>
      [...queryKeys.pwaCaches.all, 'list', projectId] as const,
  },
};

export default queryClient;
