import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import path from 'path';

export default defineConfig({
  plugins: [
    react(),
    // PWA plugin temporarily disabled to avoid service worker conflicts
    // VitePWA can be re-enabled later for production PWA features
  ],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),
      '@components': path.resolve(__dirname, './src/components'),
      '@pages': path.resolve(__dirname, './src/pages'),
      '@hooks': path.resolve(__dirname, './src/hooks'),
      '@stores': path.resolve(__dirname, './src/stores'),
      '@services': path.resolve(__dirname, './src/services'),
      '@utils': path.resolve(__dirname, './src/utils'),
      '@types': path.resolve(__dirname, './src/types'),
    },
  },
  server: {
    host: '0.0.0.0', // Important for Docker
    port: 3000,
    watch: {
      // Use polling for Docker/Kubernetes compatibility
      usePolling: true,
      ignored: [
        '**/node_modules/**',
        '**/dist/**',
        '**/.git/**',
      ],
    },
    proxy: {
      '/api': {
        target: process.env.VITE_API_URL !== undefined
                ? (process.env.VITE_API_URL || '')  // Use value or empty string
                : (process.env.NODE_ENV === 'development' && process.env.KUBERNETES_SERVICE_HOST
                    ? 'http://gateway-service:8000'  // Kubernetes internal service
                    : 'http://localhost:8000'),      // Local development
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''),
      },
    },
  },
  build: {
    outDir: 'dist',
    sourcemap: true,
    rollupOptions: {
      external: ['/runtime-config.js'], // Externalize runtime config to avoid bundling
      output: {
        manualChunks: {
          vendor: ['react', 'react-dom', 'react-router-dom'],
          ui: ['@radix-ui/react-dialog', '@radix-ui/react-dropdown-menu'],
          charts: ['recharts'],
          forms: ['react-hook-form', 'zod'],
        },
      },
    },
  },
});
