import { defineConfig, loadEnv } from 'vite'
import react from '@vitejs/plugin-react'
import tailwindcss from '@tailwindcss/vite'
import path from 'path'

// https://vite.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), '')

  return {
    plugins: [
      react(),
      tailwindcss(),
    ],
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
      },
      // For npm workspaces - look for dependencies in parent node_modules
      preserveSymlinks: true,
    },
    // For npm workspaces - optimize dependencies from root
    optimizeDeps: {
      include: ['react', 'react-dom', 'react-markdown'],
    },
    server: {
      host: true, // Listen on all addresses
      port: 5173,
      strictPort: true,
      proxy: {
        '/ingest': {
          target: env.VITE_PUBLIC_POSTHOG_HOST || 'https://eu.i.posthog.com',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/ingest/, ''),
        },
      },
    },
  }
})
