// KAP DSI Hub — Service Worker v1
const CACHE = 'kapdsi-hub-v1';
// URLs relatives au scope du service worker
const STATIC = [
  './',
  './index.html',
  '../data.js',
  '../favicon.png',
];

self.addEventListener('install', e => {
  e.waitUntil(caches.open(CACHE).then(c => c.addAll(STATIC)).then(() => self.skipWaiting()));
});

self.addEventListener('activate', e => {
  e.waitUntil(clients.claim());
});

// Network-first pour l'API, cache-first pour les assets statiques
self.addEventListener('fetch', e => {
  const url = new URL(e.request.url);
  if (url.pathname.includes('/api.php')) {
    // Toujours réseau pour l'API
    e.respondWith(fetch(e.request).catch(() => new Response('{"ok":false,"error":"Hors ligne"}', {headers:{'Content-Type':'application/json'}})));
  } else {
    e.respondWith(caches.match(e.request).then(r => r || fetch(e.request)));
  }
});
