import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { VitePWA } from 'vite-plugin-pwa';

export default defineConfig({
  plugins: [
    react(),
    VitePWA({
      registerType: 'autoUpdate',
      manifest: {
        name: 'Bridzik',
        short_name: 'Bridzik',
        theme_color: '#1e3a5f',
        background_color: '#0f172a',
        display: 'standalone',
        icons: [
          { src: 'icon-192.png', sizes: '192x192', type: 'image/png' },
          { src: 'icon-512.png', sizes: '512x512', type: 'image/png' },
        ],
      },
      workbox: {
        globPatterns: ['**/*.{js,css,html,ico,png,svg}'],
      },
    }),
  ],
  server: {
    host: true,
    // Docker bind mounts on Windows/macOS don't forward native FS events into the
    // container, so Vite's watcher never fires and HMR appears "stuck". Polling the
    // mounted files makes hot reload work without restarting the container.
    watch: {
      usePolling: true,
      interval: 200,
    },
    proxy: {
      '/socket.io': {
        // Local dev defaults to localhost; docker-compose sets VITE_BACKEND_URL
        // to the backend service (http://backend:5000).
        target: process.env.VITE_BACKEND_URL ?? 'http://localhost:5000',
        ws: true,
        changeOrigin: true,
      },
      '/api': {
        // Backend HTTP endpoints (/api/track, /api/admin/stats) -- kept under
        // /api so they never collide with client-side routes like /admin/stats.
        target: process.env.VITE_BACKEND_URL ?? 'http://localhost:5000',
        changeOrigin: true,
      },
    },
  },
});
