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

  modules: [
    '@nuxt/ui',
    '@vite-pwa/nuxt'
  ],

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

  colorMode: {
    classSuffix: '',
    preference: 'dark',
    fallback: 'dark'
  },

  app: {
    head: {
      link: [
        { rel: 'icon', type: 'image/png', href: '/icons/icon-192.png' },
        { rel: 'apple-touch-icon', sizes: '192x192', href: '/apple-touch-icon.png' },
        { rel: 'manifest', href: '/manifest.webmanifest' }
      ],
      meta: [
        { name: 'theme-color', content: '#3f75d2' },
        { name: 'apple-mobile-web-app-capable', content: 'yes' },
        { name: 'mobile-web-app-capable', content: 'yes' },
        { name: 'apple-mobile-web-app-status-bar-style', content: 'black-translucent' }
      ]
    }
  },

  nitro: {
    routeRules: {
      '/manifest.webmanifest': {
        headers: {
          'Content-Type': 'application/manifest+json',
          'Cache-Control': 'public, max-age=3600'
        }
      },
      '/sw.js': {
        headers: {
          'Service-Worker-Allowed': '/',
          'Cache-Control': 'public, max-age=0'
        }
      }
    }
  },

  runtimeConfig: {
    // Variables privadas del servidor (no expuestas al cliente)
    printerHost: process.env.PRINTER_HOST || '192.168.87.147',
    printerDeviceId: process.env.PRINTER_DEVICE_ID || 'matricial2',
    printerTimeoutMs: process.env.PRINTER_TIMEOUT_MS || '60000',

    public: {
      // Variables públicas (expuestas al cliente)
      authentikUrl: process.env.NUXT_PUBLIC_AUTHENTIK_URL || 'https://authentik.nucleoriofrio.com',
      authEnabled: process.env.NUXT_PUBLIC_AUTH_ENABLED === 'true'
    }
  },

  pwa: {
    registerType: 'autoUpdate',
    strategies: 'generateSW',
    manifestFilename: 'manifest.webmanifest',
    includeAssets: [
      'favicon.ico',
      'icon.png',
      'apple-touch-icon.png',
      'icons/*.png'
    ],
    manifest: {
      id: '/?app=printercentral',
      name: 'Printer Central',
      short_name: 'Printer',
      description: 'Administra tus impresoras de forma facil y rapida.',
      theme_color: '#3f75d2',
      background_color: '#1a1a2e',
      display: 'standalone',
      orientation: 'portrait',
      scope: '/',
      start_url: '/',
      launch_handler: {
        client_mode: 'navigate-existing'
      },
      icons: [
        {
          src: '/icons/icon-72.png',
          sizes: '72x72',
          type: 'image/png'
        },
        {
          src: '/icons/icon-96.png',
          sizes: '96x96',
          type: 'image/png'
        },
        {
          src: '/icons/icon-128.png',
          sizes: '128x128',
          type: 'image/png'
        },
        {
          src: '/icons/icon-144.png',
          sizes: '144x144',
          type: 'image/png'
        },
        {
          src: '/icons/icon-152.png',
          sizes: '152x152',
          type: 'image/png'
        },
        {
          src: '/icons/icon-192.png',
          sizes: '192x192',
          type: 'image/png',
          purpose: 'any'
        },
        {
          src: '/icons/icon-384.png',
          sizes: '384x384',
          type: 'image/png'
        },
        {
          src: '/icons/icon-512.png',
          sizes: '512x512',
          type: 'image/png',
          purpose: 'any maskable'
        }
      ]
    },
    workbox: {
      navigateFallback: '/',
      navigateFallbackDenylist: [/^\/api\//],
      globPatterns: ['**/*.{js,css,html,png,svg,ico,json,jpeg,webp,woff2}'],
      cleanupOutdatedCaches: true,
      maximumFileSizeToCacheInBytes: 4 * 1024 * 1024,
      runtimeCaching: [
        {
          // API calls siempre van a la red
          urlPattern: ({ url }) => url.pathname.startsWith('/api/'),
          handler: 'NetworkOnly'
        },
        {
          // Páginas con NetworkFirst
          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 días
            },
            cacheableResponse: {
              statuses: [0, 200]
            }
          }
        },
        {
          // Assets estáticos con CacheFirst
          urlPattern: ({ url, request }) => {
            return request.destination === 'image' ||
                   request.destination === 'style' ||
                   request.destination === 'script'
          },
          handler: 'CacheFirst',
          options: {
            cacheName: 'assets-cache',
            expiration: {
              maxEntries: 100,
              maxAgeSeconds: 60 * 60 * 24 * 30 // 30 días
            }
          }
        }
      ]
    },
    client: {
      installPrompt: true,
      periodicSyncForUpdates: 3600
    },
    devOptions: {
      enabled: process.env.NODE_ENV === 'development',
      type: 'module'
    }
  }
})
