import { defineConfig, type ProxyOptions } from 'vite'
import react from '@vitejs/plugin-react'

const API_TARGET = process.env.API_PROXY_TARGET ?? 'http://localhost:8081'

// Proxy API calls through the dev server so the dashboard is same-origin.
// Expo's dev-server CORS middleware rejects foreign Origin headers, so
// the proxy strips them before forwarding.
const proxy: Record<string, ProxyOptions> = {}
for (const path of ['/auth', '/user', '/ride', '/driver', '/admin']) {
  proxy[path] = {
    target: API_TARGET,
    changeOrigin: true,
    configure: (p) => {
      p.on('proxyReq', (req) => {
        req.removeHeader('origin')
      })
    },
  }
}

export default defineConfig({
  plugins: [react()],
  server: { proxy },
})
