const CACHE = 'notifypulse-v3';
const PRECACHE = ['/pwa/', '/pwa/index.html', '/pwa/manifest.json'];

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

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

self.addEventListener('fetch', e => {
  const url = new URL(e.request.url);
  // Always hit network for API calls
  if (url.pathname.startsWith('/api/')) return;
  // Cache-first for PWA assets
  e.respondWith(
    caches.match(e.request).then(r => r || fetch(e.request))
  );
});