import path from 'path';
import { defineConfig, loadEnv } from 'vite';
import react from '@vitejs/plugin-react';
import basicSsl from '@vitejs/plugin-basic-ssl';

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, '.', '');
  return {
    server: {
      port: 3000,
      host: '0.0.0.0',
      proxy: {
        // TMS Permission API (TypeScript backend on port 4000)
        '/api/submit': {
          target: 'http://127.0.0.1:4000',
          changeOrigin: true,
          secure: false,
        },
        '/api/submissions': {
          target: 'http://127.0.0.1:4000',
          changeOrigin: true,
          secure: false,
        },
        '/api/usernames': {
          target: 'http://127.0.0.1:4000',
          changeOrigin: true,
          secure: false,
        },
        '/api/user/search': {
          target: 'http://127.0.0.1:4000',
          changeOrigin: true,
          secure: false,
        },
        '/api/user/resolve-emails': {
          target: 'http://127.0.0.1:4000',
          changeOrigin: true,
          secure: false,
        },
        '/api/project-info': {
          target: 'http://127.0.0.1:4000',
          changeOrigin: true,
          secure: false,
        },
        '/api/project-member': {
          target: 'http://127.0.0.1:4000',
          changeOrigin: true,
          secure: false,
        },
        '/api/queue': {
          target: 'http://127.0.0.1:4000',
          changeOrigin: true,
          secure: false,
        },
        // Download API (Python backend on port 8000)
        '/api/downloads': {
          target: 'http://127.0.0.1:8000',
          changeOrigin: true,
          secure: false,
        },
        '/api/raw': {
          target: 'http://127.0.0.1:8000',
          changeOrigin: true,
          secure: false,
        },
        '/api/nas': {
          target: 'http://127.0.0.1:8000',
          changeOrigin: true,
          secure: false,
        },
        '/api/custom-paths': {
          target: 'http://127.0.0.1:8000',
          changeOrigin: true,
          secure: false,
        },
        '/api/check': {
          target: 'http://127.0.0.1:4000',
          changeOrigin: true,
          secure: false,
        },
        // Fallback for other /api requests (Python backend)
        '/api': {
          target: 'http://127.0.0.1:8000',
          changeOrigin: true,
          secure: false,
        }
      }
    },
    plugins: [react(), basicSsl()],
    define: {
    },
    resolve: {
      alias: {
        '@': path.resolve(__dirname, '.'),
      }
    }
  };
});
