// Copyright 2026 CRMy Contributors
// SPDX-License-Identifier: Apache-2.0

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import path from 'path';
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
const pkg = require('./package.json') as { version?: string };

export default defineConfig({
  plugins: [
    {
      name: 'redirect-app-base',
      configureServer(server) {
        server.middlewares.use((req, res, next) => {
          const pathname = req.url?.split('?')[0];
          if (pathname === '/app') {
            res.statusCode = 308;
            res.setHeader('Location', '/app/');
            res.end();
            return;
          }
          next();
        });
      },
    },
    react(),
  ],
  define: {
    __CRMY_WEB_VERSION__: JSON.stringify(pkg.version ?? 'unknown'),
  },
  base: '/app/',
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),
      '@crmy/shared': path.resolve(__dirname, '../shared/src/index.ts'),
    },
  },
  server: {
    port: 5173,
    proxy: {
      '/api': 'http://localhost:3000',
      '/auth': 'http://localhost:3000',
      '/mcp': 'http://localhost:3000',
      '/health': 'http://localhost:3000',
    },
  },
  build: {
    outDir: 'dist',
    rollupOptions: {
      output: {
        manualChunks(id) {
          if (!id.includes('node_modules')) return undefined;
          if (id.includes('/react/') || id.includes('/react-dom/') || id.includes('/react-router-dom/')) {
            return 'vendor-react';
          }
          if (id.includes('@radix-ui')) return 'vendor-radix';
          if (id.includes('recharts') || id.includes('@xyflow')) return 'vendor-visuals';
          if (id.includes('lucide-react')) return 'vendor-icons';
          return undefined;
        },
      },
    },
  },
});
