// 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: {
    // Variables privadas del servidor (no expuestas al cliente)
    authentikApiToken: process.env.NUXT_AUTHENTIK_API_TOKEN || '',
    authentikApiUrl: process.env.NUXT_AUTHENTIK_API_URL || 'https://authentik.nucleoriofrio.com',

    // Metabase API (para contactos)
    metabaseApiUrl: process.env.NUXT_METABASE_API_URL || 'http://metabase:3000',
    metabaseApiKey: process.env.NUXT_METABASE_API_KEY || '',
    metabaseDatabaseId: parseInt(process.env.NUXT_METABASE_DATABASE_ID || '2'),
    metabaseTableId: parseInt(process.env.NUXT_METABASE_TABLE_ID || '15'),

    // Directorio de datos para aliases de contactos
    dataDir: process.env.NUXT_DATA_DIR || './data',

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

  pwa: {
    registerType: 'autoUpdate',
    includeAssets: ['favicon.ico', 'apple-touch-icon.png', 'icon.svg', 'offline.html'],
    manifest: {
      id: '/?app=inicio',
      name: 'Perfil Nucleo',
      short_name: 'Perfil',
      description: 'Gestiona tu perfil de usuario y accede a tus aplicaciones de Nucleo',
      lang: 'es',
      dir: 'ltr',
      theme_color: '#00DC82',
      background_color: '#ffffff',
      display: 'standalone',
      display_override: ['window-controls-overlay', 'standalone'],
      orientation: 'any',
      scope: '/',
      start_url: '/?source=pwa',
      // Control de ventanas - mantener una sola ventana al navegar
      launch_handler: {
        client_mode: 'navigate-existing'
      },
      // Capturar todos los enlaces que apunten a esta app
      // NOTA: capture_links es experimental pero REQUERIDO para el funcionamiento correcto
      // TypeScript dará error TS2353 pero se ignora intencionalmente (ver README.md)
      capture_links: 'existing-client-navigate',
      // Share Target API - Permite compartir fotos con la PWA
      share_target: {
        action: '/api/share-target',
        method: 'POST',
        enctype: 'multipart/form-data',
        params: {
          title: 'title',
          text: 'text',
          files: [
            {
              name: 'media',
              accept: ['image/*']
            }
          ]
        }
      },
      // Extender scope a otros subdominios de Nucleo V3
      scope_extensions: [
        {
          origin: "https://musica.nucleoriofrio.com"
        },
        {
          origin: "https://docs.nucleoriofrio.com"
        },
        {
          origin: "https://analitica.nucleoriofrio.com"
        },
        {
          origin: "https://seguidordelotes.nucleoriofrio.com"
        },
        {
          origin: "https://whisper.nucleoriofrio.com"
        },
        {
          origin: "https://amigos.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'
    }
  }
})