import { createCacheManager } from './cacheManager'

/**
 * Maps archivable_type (API) to localStorage cache prefixes used by Pinia stores.
 */
const ARCHIVABLE_CACHE_PREFIXES = {
  'App\\Models\\Program': 'programs',
  'App\\Models\\AdditionalActivity': 'activities',
  'App\\Models\\Page': 'pages',
  'App\\Models\\Form': 'forms',
  'App\\Models\\Testimonial': 'testimonials',
  'App\\Models\\Teacher': 'teachers',
  'App\\Models\\User': 'users',
  'App\\Models\\PageTemplate': 'pagetemplates',
  'App\\Models\\FranchiseeGroup': 'franchisee-groups',
  'App\\Models\\Setting': 'settings',
  'App\\Models\\Media': 'media',
  'App\\Models\\Franchisee': 'franchisees',
  'App\\Models\\FormSubmission': 'approvals',
}

/**
 * Clear frontend list caches for a restored/deleted archivable type.
 */
export function clearEntityCachesForType(archivableType) {
  if (!archivableType) return

  const prefix = ARCHIVABLE_CACHE_PREFIXES[archivableType]
  if (prefix) {
    createCacheManager(prefix).clearAll()
  }
}

/**
 * Clear all entity list caches (e.g. after bulk operations).
 */
export function clearAllEntityCaches() {
  Object.values(ARCHIVABLE_CACHE_PREFIXES).forEach((prefix) => {
    createCacheManager(prefix).clearAll()
  })
}
