/* MyKollectOr® — Service Worker PWA v3
   Cache uniquement les assets statiques — jamais les appels API */

const CACHE_NAME = 'mykollector-v3';
const ASSETS = [
  './',
  './dashboard.html',
  './chrono.html',
  './manifest.json'
];

self.addEventListener('install', function(e){
  e.waitUntil(
    caches.open(CACHE_NAME).then(function(cache){
      return cache.addAll(ASSETS);
    })
  );
  self.skipWaiting();
});

self.addEventListener('activate', function(e){
  e.waitUntil(
    caches.keys().then(function(keys){
      return Promise.all(
        keys.filter(k => k !== CACHE_NAME).map(k => caches.delete(k))
      );
    })
  );
  self.clients.claim();
});

self.addEventListener('fetch', function(e){
  var url = e.request.url;

  // Ne jamais intercepter les appels API
  if (url.includes('api.mykollector.com') || url.includes('fonts.googleapis') || url.includes('fonts.gstatic')) {
    e.respondWith(fetch(e.request));
    return;
  }

  // Cache-first pour les assets statiques
  e.respondWith(
    caches.match(e.request).then(function(cached){
      return cached || fetch(e.request);
    })
  );
});
