import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'

export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      '@': resolve(__dirname, 'src'),
    },
  },
  server: {
    host: '0.0.0.0',
    port: parseInt(process.env.PORT || '10038'),
    // 以下代理配置仅用于开发模式（Vite dev server），生产环境通过 Nginx 反向代理转发
    // Proxy config below is for development only (Vite dev server).
    // In production, requests are forwarded via Nginx reverse proxy.
    proxy: {
      // Java document converter service
      '/api/converter': {
        target: 'http://localhost:18800',
        changeOrigin: true,
        rewrite: (path: string) => path.replace(/^\/api\/converter/, '/api'),
      },
      // Direct /api/v1 calls → FastAPI backend
      '/api/v1': {
        target: 'http://localhost:8900',
        changeOrigin: true,
      },
      // Legacy /api/ai/v1 calls → rewrite to /api/v1 on FastAPI backend
      '/api/ai': {
        target: 'http://localhost:8900',
        changeOrigin: true,
        rewrite: (path: string) => path.replace(/^\/api\/ai/, '/api'),
      },
      '/api/biz': {
        target: 'http://localhost:8900',
        changeOrigin: true,
        rewrite: (path: string) => path.replace(/^\/api\/biz/, '/api'),
      },
    },
  },
  css: {
    preprocessorOptions: {
      less: {
        javascriptEnabled: true,
      },
    },
  },
})
