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

module.exports = function(app) {
  // Proxy /uploads requests to backend
  app.use(
    '/uploads',
    createProxyMiddleware({
      target: process.env.REACT_APP_API_BASE_URL || 'http://localhost:8080',
      changeOrigin: true,
      logLevel: 'debug',
      onError: (err, req, res) => {
        console.error('Proxy error for /uploads:', err);
      },
    })
  );

  // Proxy /static requests to backend (for any static assets served by Go)
  app.use(
    '/static',
    createProxyMiddleware({
      target: process.env.REACT_APP_API_BASE_URL || 'http://localhost:8080',
      changeOrigin: true,
      logLevel: 'debug',
      onError: (err, req, res) => {
        console.error('Proxy error for /static:', err);
      },
    })
  );

  // Proxy /cache requests to backend (for FACR cache files, etc.)
  app.use(
    '/cache',
    createProxyMiddleware({
      target: process.env.REACT_APP_API_BASE_URL || 'http://localhost:8080',
      changeOrigin: true,
      logLevel: 'debug',
      onError: (err, req, res) => {
        console.error('Proxy error for /cache:', err);
      },
    })
  );
};
