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

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  server: {
    port: {{PORT}},
    proxy: {
      // SSE events endpoint — must disable buffering for streaming
      '/api/{{SERVICE_NAME}}/events': {
        target: 'http://localhost:{{SERVICE_PORT}}',
        changeOrigin: true,
        // Disable response buffering so SSE events stream immediately
        configure: (proxy) => {
          proxy.on('proxyRes', (proxyRes) => {
            // Prevent Vite from buffering SSE responses
            if (proxyRes.headers['content-type']?.includes('text/event-stream')) {
              proxyRes.headers['cache-control'] = 'no-cache';
              proxyRes.headers['x-accel-buffering'] = 'no';
            }
          });
        },
      },
      '/api': {
        target: 'http://localhost:{{SERVICE_PORT}}',
        changeOrigin: true,
      },
    },
  },
  preview: {
    port: {{PORT}},
  },
});
