/**
 * electron-vite Configuration — Bradly Desktop App
 *
 * Configures three build targets:
 * - main: Electron main process (Node.js)
 * - preload: Sandboxed bridge scripts
 * - renderer: React app (browser)
 */
import { defineConfig, externalizeDepsPlugin } from 'electron-vite';
import react from '@vitejs/plugin-react';
import tailwindcss from '@tailwindcss/vite';
import path from 'path';

export default defineConfig({
  // ═══ Main Process (Node.js environment) ═══
  main: {
    plugins: [externalizeDepsPlugin()],
    build: {
      outDir: 'out/main',
      rollupOptions: {
        input: {
          index: path.resolve(__dirname, 'src/electron/main.ts'),
        },
        // Keep Remotion + Express packages external (they have native deps)
        external: [
          '@remotion/bundler',
          '@remotion/renderer',
          /^@remotion\/compositor/,
          'express',
          'multer',
          'vite',
        ],
      },
    },
  },

  // ═══ Preload Scripts (sandboxed bridge) ═══
  preload: {
    plugins: [externalizeDepsPlugin()],
    build: {
      outDir: 'out/preload',
      rollupOptions: {
        input: {
          index: path.resolve(__dirname, 'src/electron/preload.ts'),
        },
      },
    },
  },

  // ═══ Renderer (React app — browser environment) ═══
  renderer: {
    root: '.',
    plugins: [react(), tailwindcss()],
    build: {
      outDir: 'out/renderer',
      rollupOptions: {
        input: {
          index: path.resolve(__dirname, 'index.html'),
        },
      },
    },
    resolve: {
      alias: {
        '@': path.resolve(__dirname, '.'),
      },
    },
    server: {
      hmr: process.env.DISABLE_HMR !== 'true',
      watch: process.env.DISABLE_HMR === 'true' ? null : {},
      // Proxy API calls to the embedded Express server
      proxy: {
        '/api': {
          target: 'http://127.0.0.1:3000',
          changeOrigin: true,
        },
      },
    },
  },
});
