import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { execSync } from 'child_process';
import { resolve } from 'node:path';

function getAppVersion(): string {
  try {
    // stdio stderr:'ignore' keeps git's "fatal: not a git repository" off the
    // build output when building from an exported tree (e.g. the OSS sync stage).
    return execSync("TZ=UTC git log -1 --format=%cd --date=format:'%Y%m%d.%H%M%S'", {
      encoding: 'utf-8',
      stdio: ['ignore', 'pipe', 'ignore'],
    }).trim();
  } catch {
    return 'dev';
  }
}

export default defineConfig({
  plugins: [react()],
  base: '/ui/',
  define: {
    __APP_VERSION__: JSON.stringify(getAppVersion()),
  },
  build: {
    outDir: 'dist',
    rollupOptions: {
      input: {
        main: resolve(__dirname, 'index.html'),
        appHarness: resolve(__dirname, 'app-harness.html'),
      },
    },
  },
  server: {
    proxy: {
      '/api': 'http://localhost:9876',
    },
  },
});
