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

// https://vite.dev/config/
export default defineConfig({
  plugins: [react()],
  server: {
    // Security headers for development server
    headers: {
      "X-Content-Type-Options": "nosniff",
      "X-Frame-Options": "DENY",
      "X-XSS-Protection": "1; mode=block",
      "Referrer-Policy": "strict-origin-when-cross-origin",
      "Permissions-Policy": "geolocation=(), microphone=(), camera=()",
    },
    proxy: {
      // Proxy all API requests to backend server
      "/api": {
        target: "http://localhost:8000",
        changeOrigin: true,
        secure: false,
      },
      // Legacy proxy for MapleFile API requests (kept for backward compatibility)
      "/maplefile/api": {
        target: "http://localhost:8000",
        changeOrigin: true,
        secure: false,
        rewrite: (path) => path.replace(/^\/maplefile/, ""),
      },
      // Legacy proxy for IAM API requests (kept for backward compatibility)
      "/iam/api": {
        target: "http://localhost:8000",
        changeOrigin: true,
        secure: false,
        rewrite: (path) => path.replace(/^\/iam/, ""),
      },
    },
  },
  // Production build security optimizations
  build: {
    // Enable minification for production (using esbuild)
    minify: "esbuild",
    // Generate source maps for debugging (but not inline)
    sourcemap: false,
    // Warn about large chunks
    chunkSizeWarningLimit: 1000,
  },
  // Additional esbuild options for security
  esbuild: {
    // Remove console.log and debugger in production
    drop: process.env.NODE_ENV === 'production' ? ['console', 'debugger'] : [],
  },
});
