import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import tailwindcss from '@tailwindcss/vite'
import { execSync } from 'child_process'

// Derive version from git tags; fall back to commit hash
function getGitVersion() {
  try {
    // If there's a tag pointing at HEAD, use it exactly (e.g. "v1.2.0")
    // Otherwise use "v0.0.0-<hash>[-dirty]"
    const raw = execSync('git describe --tags --always --dirty', { stdio: ['pipe', 'pipe', 'ignore'] })
      .toString()
      .trim();
    // If it looks like a pure tag (no dashes after the tag part) return as-is
    return raw;
  } catch {
    return 'dev';
  }
}

const APP_VERSION = getGitVersion();
console.log(`[vite] App version: ${APP_VERSION}`);

// https://vite.dev/config/
export default defineConfig({
  plugins: [react(), tailwindcss()],
  define: {
    // Injected at build time — available as __APP_VERSION__ in source
    __APP_VERSION__: JSON.stringify(APP_VERSION),
  },
  server: {
    proxy: {
      '/api': {
        target: 'http://localhost:5000',
        changeOrigin: true,
      }
    }
  }
})
