import path from "node:path";
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";

export default defineConfig({
  plugins: [react()],
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"),
    },
  },
  server: {
    port: 5173,
    host: true,
    // Proxy /api/* to the FastAPI backend so relative-URL fetchers
    // (e.g. the live-tail NDJSON streams opened by src/lib/tail-stream.ts)
    // resolve through the same origin as the Vite dev server instead of
    // 404'ing. The backend's own port is taken from CYCLONE_PORT (default
    // 8000) — keeping that env-driven so deploys don't have to edit
    // both files.
    proxy: {
      "/api": {
        target: `http://127.0.0.1:${process.env.CYCLONE_PORT ?? "8000"}`,
        changeOrigin: true,
        // SSE/NDJSON streams: disable response buffering so the proxy
        // doesn't hold lines until the chunk size fills. uvicorn already
        // streams as `transfer-encoding: chunked`, but axios-style proxy
        // servers can buffer; the explicit headers keep browsers honest.
        configure: (proxy) => {
          proxy.on("proxyRes", (proxyRes) => {
            proxyRes.headers["x-accel-buffering"] = "no";
          });
        },
      },
    },
  },
});
