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

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  server: {
    port: 3001,
    proxy: {
      // SSE events endpoint — must disable buffering for streaming
      '/api/persona-api/events': {
        target: 'http://localhost:0',
        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:0',
        changeOrigin: true,
      },
    },
  },
  preview: {
    port: 3001,
  },
});
