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

// 默认走 Go 后端 :4001；要回退 Node 后端就 set VITE_API_TARGET=http://localhost:4000
export default defineConfig(({ command, mode }) => {
  const env = loadEnv(mode, process.cwd(), '');
  const target = env.VITE_API_TARGET || 'https://api.hoyidata.com';

  return {
    // 本地开发走根路径，生产构建部署到 /aura 子路径
    base: command === 'serve' ? '/' : '/aura/',
    plugins: [react()],
    server: {
      port: 3001,
      proxy: {
        '/api': {
          target,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, '/aura/v1'),
          // SSE 不要被压缩；保持长连接
          configure: (proxy) => {
            proxy.on('proxyReq', (proxyReq) => {
              proxyReq.setHeader('Accept-Encoding', 'identity');
            });
          }
        }
      }
    }
  };
});

