import { defineConfig } from 'vite';
import { sveltekit } from '@sveltejs/kit/vite';
import { SvelteKitPWA } from '@vite-pwa/sveltekit';
import { paraglide } from '@inlang/paraglide-sveltekit/vite';

export default defineConfig({
	plugins: [
		paraglide({
			project: './project.inlang',
			outdir: './src/lib/paraglide'
		}),
		sveltekit(),
		SvelteKitPWA({
			// strategies defaults to 'generateSW' — the plugin auto-generates the service
			// worker. We do NOT use 'injectManifest' here because SvelteKit's own Vite
			// plugin intercepts src/service-worker.ts and blocks external imports (Workbox).
			// In Fase 2b we will introduce a custom SW as src/sw.ts (a filename SvelteKit
			// does not recognise as a service worker) and switch to injectManifest then.
			scope: '/',
			base: '/',
			manifest: {
				name: 'Colectivo',
				short_name: 'Colectivo',
				description: 'Gestión colaborativa del hogar',
				theme_color: '#0f172a',
				background_color: '#f7f9fb',
				display: 'standalone',
				orientation: 'portrait',
				scope: '/',
				start_url: '/',
				icons: [
					{ src: '/icons/icon-192.png', sizes: '192x192', type: 'image/png' },
					{ src: '/icons/icon-512.png', sizes: '512x512', type: 'image/png' },
					{ src: '/icons/icon-512.png', sizes: '512x512', type: 'image/png', purpose: 'maskable' }
				]
			},
			workbox: {
				globPatterns: ['client/**/*.{js,css,ico,png,svg,webp,woff,woff2}']
			},
			devOptions: {
				enabled: true,
				suppressWarnings: true,
				type: 'module',
				navigateFallback: '/'
			}
		})
	],
	server: {
		port: 5173,
		// Bind to all interfaces so the LAN (phone, tablet, other laptops on
		// the same Wi-Fi) can reach the dev server at http://<LAN-IP>:5173.
		// Auth flows will fail from non-laptop clients because Keycloak's
		// issuer URL resolves to `keycloak:8080` only on the laptop's /etc/hosts
		// — use this for layout/CSS visual checks, not for full login tests.
		host: true
	}
});
