import { defineConfig } from 'vite'
import laravel from 'laravel-vite-plugin'
import vue from '@vitejs/plugin-vue'
import tailwindcss from '@tailwindcss/vite'
import { VitePWA } from 'vite-plugin-pwa'

// Cambia en cada build: fuerza a Workbox a re-descargar los shells de
// /login, /tdp y /nuevo-reporte (ver additionalManifestEntries mas abajo)
// en cada deploy. Una revision fija nunca se refresca, y esos shells
// terminarian referenciando JS/CSS ya borrados de un build anterior.
const offlineShellRevision = String(Date.now())

export default defineConfig({
        server: {
        host: '0.0.0.0',
        port: 5173,
        strictPort: true,
        hmr: {
            host: 'localhost',
            port: 5173,
        },
    },
    plugins: [
        laravel({
            input: ['resources/js/app.ts'],
            refresh: true,
        }),
        vue({
            template: {
                transformAssetUrls: {
                    base: null,
                    includeAbsolute: false,
                },
            },
        }),
        tailwindcss(),
        VitePWA({
            buildBase: '/',
            registerType: 'autoUpdate',
            injectRegister: false,
            manifest: false,
            includeAssets: ['favicon.ico', 'pwa-192x192.png', 'pwa-512x512.png'],
            workbox: {
                importScripts: ['/push-handlers.js'],
                globDirectory: 'public/build',
                globPatterns: ['**/*.{js,css,ico,png,svg,woff,woff2}'],
                swDest: 'public/sw.js',
                navigateFallback: null,
                runtimeCaching: [
                    {
                        urlPattern: /^https:\/\/fonts\.bunny\.net\/.*/i,
                        handler: 'CacheFirst',
                        options: {
                            cacheName: 'bunny-fonts-cache',
                            expiration: {
                                maxEntries: 10,
                                maxAgeSeconds: 60 * 60 * 24 * 365,
                            },
                            cacheableResponse: {
                                statuses: [0, 200],
                            },
                        },
                    },
                    {
                        urlPattern: ({ request }) => request.mode === 'navigate',
                        handler: 'NetworkFirst',
                        options: {
                            cacheName: 'pages-cache',
                            expiration: {
                                maxEntries: 30,
                                maxAgeSeconds: 60 * 60 * 24 * 7,
                            },
                            cacheableResponse: {
                                statuses: [0, 200],
                            },
                            networkTimeoutSeconds: 5,
                        },
                    },
                    {
                        urlPattern: /\/offline\/master-data/,
                        handler: 'NetworkFirst',
                        options: {
                            cacheName: 'master-data-cache',
                            expiration: {
                                maxEntries: 1,
                                maxAgeSeconds: 60 * 60 * 24,
                            },
                            cacheableResponse: {
                                statuses: [0, 200],
                            },
                        },
                    },
                ],
            },
        }),
    ],
})
