import { defineConfig } from "vite";
import react from "@vitejs/plugin-react-swc";
import path from "path";
import { componentTagger } from "lovable-tagger";

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => ({
  server: {
    host: "::",
    port: 8080,
    // Browser blocks :8080 → :8069 (CORS). Proxy /api to Odoo so fetch stays same-origin.
    proxy: {
      "/api": {
        target: "http://127.0.0.1:8069",
        changeOrigin: true,
      },
    },
    hmr: {
      overlay: false,
    },
  },
  plugins: [react(), mode === "development" && componentTagger()].filter(Boolean),
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"),
    },
  },
  build: {
    // Raise warning threshold slightly — route chunks and the charts vendor
    // bundle legitimately trend 300–500KB each and we already code-split.
    chunkSizeWarningLimit: 900,
    rollupOptions: {
      output: {
        manualChunks: {
          // Pin the React core to its own chunk — it's in the critical path on
          // every route and never changes between deploys, so users cache it.
          "vendor-react": ["react", "react-dom", "react-router-dom"],
          // React Query + auth context sit behind every authenticated page.
          "vendor-query": ["@tanstack/react-query"],
          // Recharts is large and only used on dashboard/report pages; keeping
          // it separate lets the login/landing chunks stay small.
          "vendor-charts": ["recharts"],
          // Radix/shadcn primitives are shared across most pages.
          "vendor-radix": [
            "@radix-ui/react-dialog",
            "@radix-ui/react-dropdown-menu",
            "@radix-ui/react-popover",
            "@radix-ui/react-select",
            "@radix-ui/react-tabs",
            "@radix-ui/react-toast",
            "@radix-ui/react-tooltip",
            "@radix-ui/react-accordion",
            "@radix-ui/react-checkbox",
            "@radix-ui/react-label",
            "@radix-ui/react-progress",
            "@radix-ui/react-radio-group",
            "@radix-ui/react-scroll-area",
            "@radix-ui/react-separator",
            "@radix-ui/react-slider",
            "@radix-ui/react-switch",
            "@radix-ui/react-avatar",
          ],
          "vendor-forms": ["react-hook-form", "@hookform/resolvers", "zod"],
          "vendor-icons": ["lucide-react"],
        },
      },
    },
  },
}));
