import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { rmSync } from 'fs';
import path from 'path';

// Plugin qui nettoie uniquement public/assets/ avant chaque build
const cleanAssetsPlugin = () => ({
  name: 'clean-assets',
  buildStart() {
    const dir = path.resolve(__dirname, '../backend/public/assets');
    try { rmSync(dir, { recursive: true, force: true }); } catch {}
  },
});

export default defineConfig({
  plugins: [react(), cleanAssetsPlugin()],
  test: {
    environment: 'jsdom',
    globals: true,
    setupFiles: './src/test/setup.js',
  },
  server: {
    proxy: {
      '/api': {
        target:      'http://localhost:8000',
        changeOrigin: true,
      },
    },
  },
  build: {
    outDir:    '../backend/public',
    emptyOutDir: false, // ne pas supprimer index.php
  },
});
