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

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), "");
  const apiUrl = env.VITE_API_URL || "http://localhost:8080";

  return {
    plugins: [react(), tailwindcss()],
    server: {
      port: 5173,
      proxy: {
        "/auth": apiUrl,
        "/users": apiUrl,
        "/calendars": apiUrl,
        "/events": apiUrl,
        "/contacts": apiUrl,
        "/availability": apiUrl,
        "/booking": apiUrl,
        "/api-keys": apiUrl,
        "/cal": apiUrl,
        "/openapi.json": apiUrl,
        "/docs": apiUrl,
      },
    },
    build: {
      outDir: "dist",
      sourcemap: mode !== "production",
      rollupOptions: {
        output: {
          manualChunks: {
            vendor: ["react", "react-dom", "react-router-dom"],
            date: ["date-fns"],
          },
        },
      },
    },
  };
});
