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

export default defineConfig(({ command }) => ({
  plugins: [react(), tailwindcss()],

  // Relative base so asset URLs resolve correctly from any subdirectory
  base: './',

  // Dev: root = src/ so HMR works against src/index.html
  // Build: root = . so rollup can resolve src/index.html as input
  root: command === 'serve' ? './src' : '.',

  resolve: {
    alias: { '@': path.resolve(__dirname, '.') },
  },

  server: {
    port: 3000,
    host: true,
  },

  build: {
    outDir: path.resolve(__dirname, 'assets'),
    emptyOutDir: true,
    rollupOptions: {
      input: path.resolve(__dirname, 'src/index.html'),
      output: {
        entryFileNames: 'acaab.js',
        chunkFileNames: 'acaab-[name].js',
        assetFileNames: (assetInfo) => {
          const name = assetInfo.names?.[0] ?? '';
          if (name.endsWith('.css')) return 'acaab.css';
          return 'acaab-[name][extname]';
        },
      },
    },
  },
}));
