const CACHE_NAME = 'lewel-cache-v1';
const urlsToCache = [
    '/',
    '/css/site.css',
    '/images/lewel-icon.png',
    '/manifest.json'
];

self.addEventListener('install', event => {
    event.waitUntil(
        caches.open(CACHE_NAME).then(cache => {
            return cache.addAll(urlsToCache);
        })
    );
});

self.addEventListener("fetch", function (event) {
    const url = new URL(event.request.url);

    // Hoppa över root / om du inte vill cachea den
    if (url.pathname === "/") {
        return;
    }

    // Annars cacha som vanligt
    event.respondWith(
        caches.match(event.request).then(function (response) {
            return response || fetch(event.request);
        })
    );
});

self.addEventListener('push', function (event) {
    console.log("📨 Push event mottagen!", event);

    const data = event.data ? event.data.json() : { title: "LEWEL", body: "Ny notis" };

    const options = {
        body: data.body,
        icon: '/icons/lewel-icon.png',
        badge: '/icons/lewel-icon.png'
    };

    event.waitUntil(
        self.registration.showNotification(data.title, options)
    );
});


