/**
 * Cache Invalidation Utilities
 *
 * Centralized cache invalidation to ensure consistency across all pages
 * when files are moved, deleted, restored, or modified.
 */

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

/**
 * Invalidate all file-related caches
 * Use this when files are moved between locations, deleted, restored, or modified
 */
export function invalidateAllFileCaches(queryClient: QueryClient) {
  queryClient.invalidateQueries({ queryKey: ['media-gallery'] })
  queryClient.invalidateQueries({ queryKey: ['review-queue'] })
  queryClient.invalidateQueries({ queryKey: ['review-list'] })
  queryClient.invalidateQueries({ queryKey: ['recycle-bin'] })
  queryClient.invalidateQueries({ queryKey: ['recycle-bin-stats'] })
  queryClient.invalidateQueries({ queryKey: ['downloads'] })
  queryClient.invalidateQueries({ queryKey: ['downloads-search'] })
  queryClient.invalidateQueries({ queryKey: ['stats'] })
  queryClient.invalidateQueries({ queryKey: ['dashboard'] })
  queryClient.invalidateQueries({ queryKey: ['dashboard-recent-items'] })
}

/** Remove items from media-gallery cache by file_path */
export function optimisticRemoveFromMediaGallery(
  queryClient: QueryClient,
  filePaths: Set<string>
) {
  queryClient.setQueriesData<{ media: any[]; total: number }>(
    { queryKey: ['media-gallery'] },
    (old) => {
      if (!old) return old
      const filtered = old.media.filter(m => !filePaths.has(m.file_path))
      return { ...old, media: filtered, total: old.total - (old.media.length - filtered.length) }
    }
  )
}

/** Remove items from review-queue cache by file_path */
export function optimisticRemoveFromReviewQueue(
  queryClient: QueryClient,
  filePaths: Set<string>
) {
  queryClient.setQueriesData<{ files: any[]; total: number }>(
    { queryKey: ['review-queue'] },
    (old) => {
      if (!old) return old
      const filtered = old.files.filter(f => !filePaths.has(f.file_path))
      return { ...old, files: filtered, total: old.total - (old.files.length - filtered.length) }
    }
  )
  queryClient.setQueriesData<{ files: any[]; total: number }>(
    { queryKey: ['review-list'] },
    (old) => {
      if (!old) return old
      const filtered = old.files.filter(f => !filePaths.has(f.file_path))
      return { ...old, files: filtered, total: old.total - (old.files.length - filtered.length) }
    }
  )
}

/** Remove items from recycle-bin cache by id */
export function optimisticRemoveFromRecycleBin(
  queryClient: QueryClient,
  ids: Set<string>
) {
  queryClient.setQueriesData<{ items: any[]; total: number }>(
    { queryKey: ['recycle-bin'] },
    (old) => {
      if (!old) return old
      const filtered = old.items.filter(item => !ids.has(item.id))
      return { ...old, items: filtered, total: old.total - (old.items.length - filtered.length) }
    }
  )
}

/**
 * Invalidate filter caches
 * Use this when new files are added that might introduce new platforms/sources
 */
export function invalidateFilterCaches(queryClient: QueryClient) {
  queryClient.invalidateQueries({ queryKey: ['download-filters'] })
  queryClient.invalidateQueries({ queryKey: ['review-filters'] })
}

/**
 * Invalidate all caches (files + filters)
 * Use this for operations that might affect both files and filter options
 */
export function invalidateAllCaches(queryClient: QueryClient) {
  invalidateAllFileCaches(queryClient)
  invalidateFilterCaches(queryClient)
}
