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

// RTGS 조회/관리 콘솔 (:5174). 요청 헤더 X-RTGS-DC(DC1/DC2/DC3)로 대상 센터 포트를 라우팅한다.
//   Chanel계열 8091 / LouisVuitton(admin) 8099 / Gucci 8095, 센터별 +100/+200 오프셋.
const OFFSET: Record<string, number> = { DC1: 0, DC2: 100, DC3: 200 }
const off = (req: any) => OFFSET[(req.headers?.['x-rtgs-dc'] as string) || 'DC1'] ?? 0
const routed = (basePort: number) => ({
  target: `http://localhost:${basePort}`,
  changeOrigin: true,
  router: (req: any) => `http://localhost:${basePort + off(req)}`,
})

export default defineConfig({
  plugins: [react()],
  // 배포 빌드: 콘솔(index.html) + 단말(dt.html) 두 엔트리를 한 번에 dist로.
  build: {
    rollupOptions: {
      input: { main: 'index.html', dt: 'dt.html' },
    },
  },
  server: {
    port: 5174,
    proxy: {
      '/accounts': routed(8091),
      '/inquiry': routed(8091),
      '/pay': routed(8091),
      '/meta': routed(8091),
      '/rawmessage': routed(8091),
      '/notifications': routed(8091),
      '/ledger': routed(8091),
      '/admin': routed(8099),
      '/console': routed(8099),
      '/gucci': routed(8095),
    },
  },
})
