const { createProxyMiddleware } = require("http-proxy-middleware");

/** Must match the port where combined-server (or SAT_backend) listens. Override via REACT_APP_PROXY_TARGET. */
module.exports = function (app) {
  const target =
    process.env.REACT_APP_PROXY_TARGET || "http://127.0.0.1:5012";

  // Mount on `/api` only — pathFilter is v3-only; this project resolves HPM v2 via react-scripts.
  // Proxying `/` or `/login` caused redirect loops (5012 → 3000/login → 5012/login …).
  // Express strips the mount path (`/api`) before the proxy sees the URL, so `/api/sat/...`
  // becomes `/sat/...` on the upstream unless we put `/api` back.
  app.use(
    "/api",
    createProxyMiddleware({
      target,
      changeOrigin: true,
      pathRewrite: (path) => `/api${path}`,
    })
  );
};
