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

export default defineConfig({
	plugins: [
		sveltekit(),
		SvelteKitPWA({
			srcDir: 'src',
			mode: 'development',
			strategies: 'generateSW',
			scope: '/',
			base: '/',
			manifest: {
				name: 'Classeo',
				short_name: 'Classeo',
				description: 'Application de gestion scolaire',
				theme_color: '#3b82f6',
				background_color: '#ffffff',
				display: 'standalone',
				start_url: '/',
				categories: ['education'],
				icons: [
					{
						src: 'pwa-192x192.png',
						sizes: '192x192',
						type: 'image/png'
					},
					{
						src: 'pwa-512x512.png',
						sizes: '512x512',
						type: 'image/png'
					},
					{
						src: 'pwa-512x512.png',
						sizes: '512x512',
						type: 'image/png',
						purpose: 'any maskable'
					}
				],
				shortcuts: [
					{
						name: 'Mon emploi du temps',
						short_name: 'EDT',
						url: '/dashboard/schedule',
						description: 'Consulter mon emploi du temps'
					}
				]
			},
			workbox: {
				globPatterns: ['**/*.{js,css,html,ico,png,svg,webp,woff,woff2}'],
				runtimeCaching: [
					{
						urlPattern: /\/api\/me\/schedule\//,
						handler: 'NetworkFirst',
						options: {
							cacheName: 'schedule-v1',
							expiration: {
								maxEntries: 90,
								maxAgeSeconds: 30 * 24 * 60 * 60
							},
							networkTimeoutSeconds: 5,
							cacheableResponse: {
								statuses: [0, 200]
							}
						}
					},
					{
						urlPattern: /\/api\/me\/children\/.*\/schedule\//,
						handler: 'NetworkFirst',
						options: {
							cacheName: 'parent-schedule-v1',
							expiration: {
								maxEntries: 180,
								maxAgeSeconds: 30 * 24 * 60 * 60
							},
							networkTimeoutSeconds: 5,
							cacheableResponse: {
								statuses: [0, 200]
							}
						}
					},
					{
						urlPattern: /\/api\/me\/children\/schedule\/summary/,
						handler: 'NetworkFirst',
						options: {
							cacheName: 'parent-schedule-v1',
							expiration: {
								maxEntries: 10,
								maxAgeSeconds: 30 * 24 * 60 * 60
							},
							networkTimeoutSeconds: 5,
							cacheableResponse: {
								statuses: [0, 200]
							}
						}
					},
					{
						urlPattern: /\/api\/me\/children$/,
						handler: 'NetworkFirst',
						options: {
							cacheName: 'parent-children-v1',
							expiration: {
								maxEntries: 10,
								maxAgeSeconds: 7 * 24 * 60 * 60
							},
							networkTimeoutSeconds: 5,
							cacheableResponse: {
								statuses: [0, 200]
							}
						}
					}
				]
			},
			devOptions: {
				enabled: false,
				type: 'module',
				navigateFallback: '/'
			}
		// eslint-disable-next-line @typescript-eslint/no-explicit-any
		}) as any
	],
	test: {
		include: ['src/**/*.{test,spec}.{js,ts}', 'tests/**/*.{test,spec}.{js,ts}'],
		globals: true,
		environment: 'jsdom',
		server: {
			deps: {
				inline: [/svelte/]
			}
		},
		alias: {
			$lib: '/src/lib',
			$app: '/node_modules/@sveltejs/kit/src/runtime/app'
		}
	},
	resolve: {
		conditions: ['browser']
	},
	server: {
		host: '0.0.0.0',
		port: 5173,
		strictPort: true,
		// Autorise les sous-domaines pour le multi-tenant (dev + prod)
		allowedHosts: ['.classeo.local', '.classeo.fr', 'localhost']
	},
	preview: {
		host: '0.0.0.0',
		port: 4173,
		strictPort: true,
		// Autorise les sous-domaines pour les tests E2E multi-tenant
		allowedHosts: ['.classeo.local', '.classeo.fr', 'localhost']
	}
});
