// https://nuxt.com/docs/api/configuration/nuxt-config
import { disableImportProtection } from './nuxt.config.workaround'

export default defineNuxtConfig({
  ssr: true,
  compatibilityDate: '2025-07-15',
  devtools: { enabled: true },
  css: ['~/assets/css/main.css'],
  modules: ['@nuxt/image', '@nuxt/ui', '@nuxt/test-utils', '@vite-pwa/nuxt'],

  // Performance optimizations
  experimental: {
    payloadExtraction: false,
    viewTransition: true
  },

  // Optimize build
  vite: {
    plugins: [disableImportProtection()],
    server: {
      hmr: {
        clientPort: 443,
        protocol: 'wss'
      },
      allowedHosts: [
        '.nucleoriofrio.com',
        'devserver.nucleoriofrio.com',
        'analitica.nucleoriofrio.com'
      ]
    },
    build: {
      cssCodeSplit: true,
      rollupOptions: {
        output: {
          manualChunks: {
            'vendor-ui': ['@nuxt/ui']
          }
        }
      }
    }
  },
  app: {
    baseURL: process.env.BASE_URL || '/',
    head: {
      link: [
        { rel: 'icon', type: 'image/png', href: `${process.env.BASE_URL || ''}/icons/icon-192.png` },
        { rel: 'apple-touch-icon', sizes: '192x192', href: `${process.env.BASE_URL || ''}/apple-touch-icon.png` },
        { rel: 'manifest', href: `${process.env.BASE_URL || ''}/manifest.webmanifest` }
      ],
      meta: [
        { name: 'theme-color', content: '#1b1209' },
        { 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: {
    baseURL: process.env.BASE_URL || '/',
    experimental: {
      openAPI: true
    },
    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'
        }
      }
    }
  },

  pwa: {
    registerType: 'autoUpdate',
    strategies: 'generateSW',
    manifestFilename: 'manifest.webmanifest',
    manifest: {
      id: '/?app=analitica',
      name: 'Analítica Núcleo Data Studio',
      short_name: 'Analítica',
      description: 'Explora y valida tus tablas Supabase desde un único panel en modo lectura.',
      start_url: process.env.BASE_URL || '/',
      scope: process.env.BASE_URL || '/',
      display: 'standalone',
      background_color: '#1b1209',
      theme_color: '#c08040',
      // Control de ventanas - mantener una sola ventana
      launch_handler: {
        client_mode: 'navigate-existing'
      },
      // Capturar enlaces dentro de la app
      capture_links: 'existing-client-navigate',
      // URL handling - nuevo estándar para manejar links a esta PWA
      handle_links: 'preferred',
      url_handlers: [
        { origin: 'https://analitica.nucleoriofrio.com' }
      ],
      icons: [
        { src: '/icons/icon-192.png', sizes: '192x192', type: 'image/png' },
        { src: '/icons/icon-512.png', sizes: '512x512', type: 'image/png' },
        { src: '/icons/icon-192-maskable.png', sizes: '192x192', type: 'image/png', purpose: 'maskable' },
        { src: '/icons/icon-512-maskable.png', sizes: '512x512', type: 'image/png', purpose: 'maskable' },
        { src: '/icons/icon-192-black.png', sizes: '192x192', type: 'image/png' },
        { src: '/icons/icon-512-black.png', sizes: '512x512', type: 'image/png' }
      ],
      screenshots: [
        {
          src: '/screenshots/screenshot-desktop.png',
          sizes: '2048x1041',
          type: 'image/png',
          form_factor: 'wide',
          label: 'Dashboard en escritorio'
        },
        {
          src: '/screenshots/screenshot-mobile.png',
          sizes: '774x1459',
          type: 'image/png',
          form_factor: 'narrow',
          label: 'Vista móvil'
        }
      ]
    },
    workbox: {
      globPatterns: ['**/*.{js,css,html,png,svg,webp,ico,json,woff2}'],
      navigateFallback: undefined,
      cleanupOutdatedCaches: true,
      maximumFileSizeToCacheInBytes: 4 * 1024 * 1024
    },
    client: {
      installPrompt: true,
      periodicSyncForUpdates: 3600
    },
    devOptions: {
      enabled: process.env.NODE_ENV === 'development',
      type: 'module'
    },
    includeAssets: [
      'favicon.ico',
      'apple-touch-icon.png',
      'icons/icon-192.png',
      'icons/icon-512.png',
      'icons/icon-192-maskable.png',
      'icons/icon-512-maskable.png',
      'icons/icon-192-black.png',
      'icons/icon-512-black.png',
      'screenshots/screenshot-desktop.png',
      'screenshots/screenshot-mobile.png'
    ]
  },
  runtimeConfig: {
    // Server-side only
    // Use container name for Docker internal communication
    metabaseUrl: process.env.METABASE_URL || 'http://metabase:3000',
    metabaseApiKey: process.env.METABASE_API_KEY || '',
    metabaseEmail: process.env.METABASE_EMAIL || 'claudeCode0@nucleoriofrio.com',
    metabasePassword: process.env.METABASE_PASSWORD || 'vK^NyZdZDH#p',
    // Public (client + server)
    public: {
      authentikUrl: process.env.NUXT_PUBLIC_AUTHENTIK_URL || 'https://authentik.nucleoriofrio.com'
    }
  }
})
