/// <reference types="vitest/config" />
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import tailwindcss from '@tailwindcss/vite';

/**
 * Vite config for the @mcpctl/web prompt editor.
 *
 * - `base: '/ui/'` so production builds work when served by mcpd at
 *   `https://mcpctl.ad.itaz.eu/ui/` via @fastify/static.
 * - Dev server proxies `/api` to mcpd so the same fetch wrapper works in
 *   both modes (in prod the UI is same-origin with mcpd, so no proxy needed).
 *   Override the dev target via `MCPCTL_API_URL` for non-default deployments.
 * - The build artifact lands in `dist/` and is consumed by
 *   `scripts/build-rpm.sh` in Stage 6.
 */
const apiTarget = process.env['MCPCTL_API_URL'] ?? 'https://mcpctl.ad.itaz.eu';

export default defineConfig({
  plugins: [react(), tailwindcss()],
  base: '/ui/',
  server: {
    port: 5173,
    proxy: {
      '/api': {
        target: apiTarget,
        changeOrigin: true,
        secure: false,
      },
    },
  },
  build: {
    outDir: 'dist',
    sourcemap: true,
    chunkSizeWarningLimit: 2500,
  },
  test: {
    environment: 'jsdom',
    globals: true,
    setupFiles: ['./tests/setup.ts'],
    include: ['tests/**/*.test.{ts,tsx}'],
  },
});
