import { defineConfig } from 'vite';
import { resolve } from 'path';
import { fileURLToPath } from 'url';
import tailwindcss from '@tailwindcss/vite';

const __dirname = resolve(fileURLToPath(import.meta.url), '..');

export default defineConfig({
  // Electron renderer process configuration
  root: 'src/renderer',
  base: './',

  build: {
    outDir: '../../dist/renderer',
    emptyOutDir: false, // Don't clear the entire dist/renderer (keep HTML)

    // Collect all CSS into a single file
    cssCodeSplit: false,

    rollupOptions: {
      input: {
        // Main entry point
        renderer: resolve(__dirname, 'src/renderer/renderer.ts')
      },
      output: {
        // Generate bundle files
        entryFileNames: 'bundle.js',
        chunkFileNames: 'bundle.js',
        assetFileNames: (assetInfo) => {
          // CSS files → bundle.css
          const name = assetInfo.names?.[0] ?? assetInfo.name ?? '';
          if (name.endsWith('.css') || assetInfo.type === 'asset' && name === '') {
            return 'bundle.css';
          }
          // Fonts, images → assets/[name].[ext]
          return 'assets/[name].[ext]';
        },
        // Use IIFE format for browser compatibility
        format: 'iife',
        name: 'PromptLineApp'
      },
      external: [] // Bundle everything
    },

    // Generate source maps for debugging
    sourcemap: true,

    // Minimize bundle size
    minify: false, // Keep readable for debugging

    // Target modern browsers (Electron)
    target: 'chrome120'
  },

  // Configure how imports are resolved
  resolve: {
    alias: {
      '@': resolve(__dirname, 'src/renderer'),
      '@utils': resolve(__dirname, 'src/renderer/utils')
    }
  },

  // Plugin configuration
  plugins: [
    tailwindcss(),
  ],

  // Development server (not used in Electron, but good to have)
  server: {
    port: 3000
  }
});
