// Service Worker for HandyBarcodeScanner PWA
const CACHE_NAME = 'scanner-v8.1';
const ASSETS = [
	'pwa.php',
	'css/scanner.css',
	'js/scanner.js',
	'img/icon-192.png',
	'img/icon-512.png',
	'https://cdn.jsdelivr.net/npm/@ericblade/quagga2@1.8.4/dist/quagga.min.js',
	'https://cdn.jsdelivr.net/npm/jsbarcode@3.11.6/dist/JsBarcode.all.min.js'
];

// Install - cache assets
self.addEventListener('install', event => {
	event.waitUntil(
		caches.open(CACHE_NAME).then(cache => {
			return cache.addAll(ASSETS);
		})
	);
	self.skipWaiting();
});

// Activate - clean old caches
self.addEventListener('activate', event => {
	event.waitUntil(
		caches.keys().then(keys => {
			return Promise.all(
				keys.filter(key => key !== CACHE_NAME).map(key => caches.delete(key))
			);
		})
	);
	self.clients.claim();
});

// Fetch - network first, fallback to cache
self.addEventListener('fetch', event => {
	const url = new URL(event.request.url);

	// Always fetch AJAX requests from network
	if (url.pathname.includes('/ajax/')) {
		event.respondWith(
			fetch(event.request).catch(err => {
				console.error('Network error for AJAX:', err);
				return new Response(JSON.stringify({
					success: false,
					error: 'Offline - Keine Netzwerkverbindung'
				}), {
					headers: {'Content-Type': 'application/json'}
				});
			})
		);
		return;
	}

	// Network first for PHP pages (to get fresh token)
	if (url.pathname.endsWith('.php')) {
		event.respondWith(
			fetch(event.request).catch(() => caches.match(event.request))
		);
		return;
	}

	// Eigene Assets: Network first, Cache als Fallback (damit Updates sofort ankommen)
	// CDN-Assets: Cache first (aendern sich nie dank versionierter URL)
	const isCDN = url.hostname !== location.hostname;

	if (isCDN) {
		event.respondWith(
			caches.match(event.request).then(cached => {
				return cached || fetch(event.request).then(response => {
					if (response.ok) {
						const clone = response.clone();
						caches.open(CACHE_NAME).then(cache => cache.put(event.request, clone));
					}
					return response;
				});
			})
		);
	} else {
		// Network first fuer eigene JS/CSS/Bilder
		event.respondWith(
			fetch(event.request).then(response => {
				if (response.ok) {
					const clone = response.clone();
					caches.open(CACHE_NAME).then(cache => cache.put(event.request, clone));
				}
				return response;
			}).catch(() => caches.match(event.request))
		);
	}
});

// Listen for messages from main app
self.addEventListener('message', event => {
	if (event.data && event.data.type === 'SKIP_WAITING') {
		self.skipWaiting();
	}
});
