// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
  compatibilityDate: '2025-07-15',
  devtools: { enabled: true },

  modules: [
    '@nuxt/ui',
    '@nuxt/test-utils',
    '@nuxt/image',
    '@nuxt/eslint',
    '@nuxt/content',
    '@vite-pwa/nuxt'
  ],

  css: ['~/assets/css/main.css'],

  runtimeConfig: {
    public: {
      authentikUrl: 'https://authentik.nucleoriofrio.com'
    }
  },

  pwa: {
    registerType: 'autoUpdate',
    includeAssets: ['favicon.ico', 'apple-touch-icon.png', 'offline.html'],
    manifest: {
      id: '/?app=riocata',
      name: 'RioCata - Sistema de Catación de Café',
      short_name: 'RioCata',
      description: 'Sistema de catación de café para evaluación y análisis de calidad',
      lang: 'es',
      dir: 'ltr',
      theme_color: '#ffffff',
      background_color: '#ffffff',
      display: 'standalone',
      display_override: ['window-controls-overlay', 'standalone'],
      orientation: 'any',
      scope: '/',
      start_url: '/?source=pwa',
      categories: ['productivity', 'business'],
      // Control de ventanas - mantener una sola ventana al navegar
      launch_handler: {
        client_mode: 'navigate-existing'
      },
      // Capturar todos los enlaces que apunten a esta app
      // @ts-ignore - Propiedad experimental no incluida en tipos oficiales
      capture_links: 'existing-client-navigate',
      // URL handling - nuevo estándar para manejar links a esta PWA
      // @ts-ignore - Propiedad experimental no incluida en tipos oficiales
      handle_links: 'preferred',
      // @ts-ignore - Propiedad experimental no incluida en tipos oficiales
      url_handlers: [
        { origin: 'https://riocata.nucleoriofrio.com' }
      ],
      icons: [
        {
          src: '/icon-72x72.png',
          sizes: '72x72',
          type: 'image/png',
          purpose: 'any'
        },
        {
          src: '/icon-96x96.png',
          sizes: '96x96',
          type: 'image/png',
          purpose: 'any'
        },
        {
          src: '/icon-128x128.png',
          sizes: '128x128',
          type: 'image/png',
          purpose: 'any'
        },
        {
          src: '/icon-144x144.png',
          sizes: '144x144',
          type: 'image/png',
          purpose: 'any'
        },
        {
          src: '/icon-152x152.png',
          sizes: '152x152',
          type: 'image/png',
          purpose: 'any'
        },
        {
          src: '/icon-192x192.png',
          sizes: '192x192',
          type: 'image/png',
          purpose: 'any'
        },
        {
          src: '/icon-256x256.png',
          sizes: '256x256',
          type: 'image/png',
          purpose: 'any'
        },
        {
          src: '/icon-384x384.png',
          sizes: '384x384',
          type: 'image/png',
          purpose: 'any'
        },
        {
          src: '/icon-512x512.png',
          sizes: '512x512',
          type: 'image/png',
          purpose: 'any'
        },
        {
          src: '/icon-512x512-maskable.png',
          sizes: '512x512',
          type: 'image/png',
          purpose: 'maskable'
        }
      ],
      screenshots: [
        {
          src: '/screenshots/desktop-1.png',
          sizes: '1920x1080',
          type: 'image/png',
          form_factor: 'wide',
          label: 'Pantalla principal en escritorio'
        },
        {
          src: '/screenshots/mobile-1.png',
          sizes: '614x853',
          type: 'image/png',
          form_factor: 'narrow',
          label: 'Pantalla principal en móvil'
        }
      ]
    },
    workbox: {
      navigateFallback: '/offline.html',
      navigateFallbackDenylist: [/^\/api\//, /^\/authentik\//],
      globPatterns: ['**/*.{js,css,html,png,svg,ico,json}'],
      cleanupOutdatedCaches: true,
      // Capturar todas las navegaciones dentro del scope
      navigateFallbackAllowlist: [/^\//],
      clientsClaim: true,
      skipWaiting: true,
      runtimeCaching: [
        {
          urlPattern: /^https:\/\/authentik\.nucleoriofrio\.com\/.*/i,
          handler: 'NetworkOnly'
        },
        {
          urlPattern: ({ url, request }) => {
            return request.destination === 'document' ||
                   request.mode === 'navigate' ||
                   url.pathname === '/'
          },
          handler: 'NetworkFirst',
          options: {
            cacheName: 'pages-cache',
            networkTimeoutSeconds: 3,
            expiration: {
              maxEntries: 50,
              maxAgeSeconds: 60 * 60 * 24 * 7 // 7 days
            },
            cacheableResponse: {
              statuses: [0, 200]
            }
          }
        }
      ]
    },
    devOptions: {
      enabled: true,
      type: 'module'
    }
  }
})