/**
 * MediBook AI — minimal service worker (PWA-ready baseline).
 * Network-first for everything; offline fallback only for navigations.
 * Clinical data is PHI: we deliberately do NOT cache API/HTML responses.
 */
const VERSION = 'mb-v1';

self.addEventListener('install', () => self.skipWaiting());
self.addEventListener('activate', (e) => e.waitUntil(self.clients.claim()));

self.addEventListener('fetch', (event) => {
    if (event.request.mode === 'navigate') {
        event.respondWith(
            fetch(event.request).catch(() =>
                new Response(
                    '<!doctype html><meta charset="utf-8"><title>Offline</title>' +
                    '<body style="font-family:sans-serif;display:grid;place-items:center;height:100vh">' +
                    '<div style="text-align:center"><h1>📡</h1><p>You are offline. Reconnect to continue.</p></div>',
                    { headers: { 'Content-Type': 'text/html' } }
                )
            )
        );
    }
});
