import type { AssetCategory } from '@lenserfight/utils/cdn-url'
import type {
  AssetCacheConfig,
  CategoryConfig,
} from './asset-cache-config'

const MB = 1024 * 1024

const CATEGORY_DEFAULTS: Record<AssetCategory, CategoryConfig> = {
  'brand': {
    ttl: 'immutable-1y',
    memoryMaxEntries: 20,
    memoryCeilingBytes: 4 * MB,
    maxResponseSizeBytes: 2 * MB,
    swStrategy: 'cache-first',
    precacheEligible: true,
    offlineFallbackAssetId: 'ui/placeholder-logo',
    sriEnabled: true,
    cacheTags: ['lf-immutable', 'lf-brand'],
  },
  'ui-visuals': {
    ttl: 'immutable-1y',
    memoryMaxEntries: 50,
    memoryCeilingBytes: 10 * MB,
    maxResponseSizeBytes: 2 * MB,
    swStrategy: 'cache-first',
    precacheEligible: true,
    offlineFallbackAssetId: 'ui/placeholder',
    sriEnabled: true,
    cacheTags: ['lf-immutable', 'lf-ui-visuals'],
  },
  'marketing': {
    ttl: 'immutable-1y',
    memoryMaxEntries: 10,
    memoryCeilingBytes: 4 * MB,
    maxResponseSizeBytes: 4 * MB,
    swStrategy: 'cache-first',
    precacheEligible: false,
    offlineFallbackAssetId: 'ui/placeholder',
    sriEnabled: true,
    cacheTags: ['lf-immutable', 'lf-marketing'],
  },
  'animations': {
    ttl: 'immutable-1y',
    memoryMaxEntries: 0,
    memoryCeilingBytes: 0,
    maxResponseSizeBytes: 20 * MB,
    swStrategy: 'cache-first',
    precacheEligible: false,
    offlineFallbackAssetId: null,
    sriEnabled: false,
    cacheTags: ['lf-immutable', 'lf-animations'],
  },
  'docs-media': {
    ttl: 'static-30d',
    memoryMaxEntries: 0,
    memoryCeilingBytes: 0,
    maxResponseSizeBytes: 10 * MB,
    swStrategy: 'stale-while-revalidate',
    precacheEligible: false,
    offlineFallbackAssetId: 'ui/placeholder',
    sriEnabled: false,
    cacheTags: ['lf-static-30d', 'lf-docs-media'],
  },
  'workflow-previews': {
    ttl: 'semi-7d',
    memoryMaxEntries: 20,
    memoryCeilingBytes: 6 * MB,
    maxResponseSizeBytes: 2 * MB,
    swStrategy: 'stale-while-revalidate',
    precacheEligible: false,
    offlineFallbackAssetId: 'ui/placeholder',
    sriEnabled: false,
    cacheTags: ['lf-semi-7d', 'lf-workflow-previews'],
  },
  'lens-previews': {
    ttl: 'semi-24h',
    memoryMaxEntries: 50,
    memoryCeilingBytes: 10 * MB,
    maxResponseSizeBytes: 2 * MB,
    swStrategy: 'stale-while-revalidate',
    precacheEligible: false,
    offlineFallbackAssetId: 'ui/avatar-default',
    sriEnabled: false,
    cacheTags: ['lf-semi-24h', 'lf-lens-previews'],
  },
  'agent-media': {
    ttl: 'semi-24h',
    memoryMaxEntries: 100,
    memoryCeilingBytes: 15 * MB,
    maxResponseSizeBytes: 2 * MB,
    swStrategy: 'stale-while-revalidate',
    precacheEligible: false,
    offlineFallbackAssetId: 'ui/avatar-default',
    sriEnabled: false,
    cacheTags: ['lf-semi-24h', 'lf-agent-media'],
  },
  'battle-assets': {
    ttl: 'semi-1h',
    memoryMaxEntries: 30,
    memoryCeilingBytes: 8 * MB,
    maxResponseSizeBytes: 5 * MB,
    swStrategy: 'stale-while-revalidate',
    precacheEligible: false,
    offlineFallbackAssetId: 'ui/placeholder',
    sriEnabled: false,
    cacheTags: ['lf-semi-1h', 'lf-battle-assets'],
  },
  'public-static': {
    ttl: 'dynamic-no-store',
    memoryMaxEntries: 0,
    memoryCeilingBytes: 0,
    maxResponseSizeBytes: 1 * MB,
    swStrategy: 'network-only',
    precacheEligible: false,
    offlineFallbackAssetId: null,
    sriEnabled: false,
    cacheTags: [],
  },
}

export const PRODUCTION_DEFAULTS: AssetCacheConfig = {
  cdnBaseUrl: 'https://cdn.lenserfight.com',
  environment: 'production',
  manifestUrl: 'https://cdn.lenserfight.com/manifest/v1/asset-manifest.json',
  categories: CATEGORY_DEFAULTS,
  memoryCache: {
    maxSizeBytes: 50 * MB,
    defaultTTLMs: 300_000,
    maxEntries: 500,
    storeBlobs: true,
  },
  serviceWorker: {
    enabled: true,
    scope: '/',
    scriptPath: '/asset-cache-sw.js',
    precacheGroups: ['critical', 'brand', 'ui-visuals'],
    manifestPollIntervalMs: 60_000,
    autoSkipWaiting: false,
    oldNamespaceRetentionMs: 0,
  },
  prefetch: {
    enabled: true,
    minBandwidthForLazy: '3g',
    minBandwidthForEager: '2g',
    viewportMargin: '200px',
    maxConcurrency: 4,
    routeTransitionLeadMs: 200,
  },
  offline: {
    enabled: true,
    probeUrl: 'https://cdn.lenserfight.com/public-static/health.json',
    onlineProbeIntervalMs: 300_000,
    offlineProbeIntervalMs: 30_000,
    probeTimeoutMs: 3_000,
  },
  security: {
    sriEnabled: true,
    sriAlgorithm: 'sha384',
    signedUrlMaxAgeSeconds: 900,
    signedUrlIpBound: false,
  },
  dev: {
    bypassCache: false,
    logLevel: 'error',
    inspectorEnabled: false,
  },
}

export function createAssetCacheConfig(
  overrides: Partial<AssetCacheConfig> = {},
): AssetCacheConfig {
  return {
    ...PRODUCTION_DEFAULTS,
    ...overrides,
    categories: { ...PRODUCTION_DEFAULTS.categories, ...overrides.categories },
    memoryCache: { ...PRODUCTION_DEFAULTS.memoryCache, ...overrides.memoryCache },
    serviceWorker: { ...PRODUCTION_DEFAULTS.serviceWorker, ...overrides.serviceWorker },
    prefetch: { ...PRODUCTION_DEFAULTS.prefetch, ...overrides.prefetch },
    offline: { ...PRODUCTION_DEFAULTS.offline, ...overrides.offline },
    security: { ...PRODUCTION_DEFAULTS.security, ...overrides.security },
    dev: { ...PRODUCTION_DEFAULTS.dev, ...overrides.dev },
  }
}

export const ROUTE_ASSET_GROUPS: Record<string, string[]> = {
  '/': ['brand', 'ui-visuals', 'marketing', 'route:home'],
  '/battles': ['battle-assets', 'ui-visuals', 'route:battles'],
  '/battles/:id': ['battle-assets', 'agent-media', 'route:battle-detail'],
  '/lensers': ['lens-previews', 'agent-media', 'ui-visuals'],
  '/lensers/:handle': ['agent-media', 'lens-previews', 'workflow-previews'],
  '/docs': ['docs-media', 'brand'],
  '/create': ['workflow-previews', 'ui-visuals', 'animations'],
}
