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

// Get dev proxy server from environment variable
const devProxyServer = process.env.DEV_PROXY_SERVER || process.env.VITE_API_BASE_URL || "http://localhost:5000";
if (process.env.DEV_PROXY_SERVER || process.env.VITE_API_BASE_URL) {
  console.log("Using devProxyServer from environment: ", devProxyServer);
}

// Get port from environment variable
const port = parseInt(process.env.VITE_PORT || process.env.PORT || "3001", 10);

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react(), tailwindcss()],
  server: {
    host: "0.0.0.0",
    port: port,
    proxy: {
      "^/api": {
        target: devProxyServer,
        xfwd: true,
      },
      "^/memos.api.v1": {
        target: devProxyServer,
        xfwd: true,
        rewrite: (path) => {
          // Map Connect RPC paths to REST API paths on FastAPI backend (port 5000)
          if (path.includes("InstanceService/GetInstanceProfile")) return "/api/v1/instance";
          if (path.includes("InstanceService/GetInstanceSetting")) return "/api/v1/instance/setting";
          if (path.includes("AuthService/GetCurrentUser")) return "/api/v1/auth/me";
          if (path.includes("AuthService/SignIn")) return "/api/v1/auth/signin";
          if (path.includes("AuthService/SignUp")) return "/api/v1/auth/signup";

          if (path.includes("UserService/ListUsers")) return "/api/v1/users";
          if (path.includes("UserService/CreateUser")) return "/api/v1/users";
          if (path.includes("UserService/GetUser")) return path.replace(/.*UserService\/GetUser/, "/api/v1/users");
          if (path.includes("UserService/ListUserSettings")) return "/api/v1/users/settings";

          if (path.includes("ShortcutService/ListShortcuts")) return "/api/v1/shortcuts";

          if (path.includes("MemoService/ListMemos")) return "/api/v1/memos";
          if (path.includes("MemoService/GetMemo")) return path.replace(/.*MemoService\/GetMemo/, "/api/v1/memos");
          if (path.includes("MemoService/CreateMemo")) return "/api/v1/memos";
          if (path.includes("MemoService/UpdateMemo")) return path.replace(/.*MemoService\/UpdateMemo/, "/api/v1/memos");
          if (path.includes("MemoService/DeleteMemo")) return path.replace(/.*MemoService\/DeleteMemo/, "/api/v1/memos");

          if (path.includes("AttachmentService/ListAttachments")) return "/api/v1/attachments";
          if (path.includes("AttachmentService/GetAttachment"))
            return path.replace(/.*AttachmentService\/GetAttachment/, "/api/v1/attachments");

          return path; // fallback
        },
      },
      "^/file": {
        target: devProxyServer,
        xfwd: true,
      },
    },
  },
  resolve: {
    alias: {
      "@/": `${resolve(__dirname, "src")}/`,
    },
  },
  build: {
    rollupOptions: {
      output: {
        manualChunks: {
          "utils-vendor": ["dayjs", "lodash-es"],
          "mermaid-vendor": ["mermaid"],
          "leaflet-vendor": ["leaflet", "react-leaflet"],
        },
      },
    },
  },
});
