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

export default defineConfig({
  plugins: [react(), tailwindcss()],
  server: {
    port: 3000,
    open: true,
    allowedHosts: true,
    // HMR disabled - reverse proxy (feeld.treytartt.com) doesn't forward WebSockets
    // Manual refresh required after code changes when using custom domain
    // Access http://localhost:3000 directly for HMR during development
    hmr: false,
    proxy: {
      '/api/graphql': {
        target: 'https://core.api.fldcore.com',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/graphql/, '/graphql'),
        secure: false,
        configure: (proxy) => {
          proxy.on('proxyReq', (proxyReq) => {
            // Remove browser headers that reveal this is a web client
            proxyReq.removeHeader('origin');
            proxyReq.removeHeader('referer');
            // Ensure mobile app headers are preserved
            if (!proxyReq.getHeader('user-agent')?.includes('feeld')) {
              proxyReq.setHeader('User-Agent', 'feeld-mobile');
            }
          });
        },
      },
      '/api/firebase': {
        target: 'https://securetoken.googleapis.com',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/firebase/, ''),
        secure: false,
      },
      '/api/images': {
        target: 'https://res.cloudinary.com',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/images/, ''),
        secure: false,
        configure: (proxy) => {
          proxy.on('proxyReq', (proxyReq) => {
            // Remove browser headers that trigger hotlink protection
            proxyReq.removeHeader('origin');
            proxyReq.removeHeader('referer');
            // Set mobile app headers
            proxyReq.setHeader('User-Agent', 'feeld-mobile');
            proxyReq.setHeader('Accept', '*/*');
          });
        },
      },
      '/api/fldcdn': {
        target: 'https://prod.fldcdn.com',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/fldcdn/, ''),
        secure: false,
        configure: (proxy) => {
          proxy.on('proxyReq', (proxyReq) => {
            // Remove browser headers that trigger hotlink protection
            proxyReq.removeHeader('origin');
            proxyReq.removeHeader('referer');
            proxyReq.removeHeader('sec-fetch-dest');
            proxyReq.removeHeader('sec-fetch-mode');
            proxyReq.removeHeader('sec-fetch-site');
            proxyReq.removeHeader('sec-ch-ua');
            proxyReq.removeHeader('sec-ch-ua-mobile');
            proxyReq.removeHeader('sec-ch-ua-platform');
            // Set mobile app headers to match iOS app
            // APP_VERSION: keep in sync with src/config/constants.ts APP_VERSION
            proxyReq.setHeader('User-Agent', 'Feeld/8.8.3 (com.3nder.ios; build:1; iOS 18.6.2) Alamofire/5.9.1');
            proxyReq.setHeader('Accept', '*/*');
            proxyReq.setHeader('Accept-Language', 'en-US,en;q=0.9');
            proxyReq.setHeader('x-app-version', '8.8.3');
            proxyReq.setHeader('x-device-os', 'ios');
            proxyReq.setHeader('x-os-version', '18.6.2');
          });
        },
      },
      // Local backend endpoints (must be last to not override specific proxies above)
      '/api/who-liked-you': {
        target: 'http://localhost:3001',
        changeOrigin: true,
      },
      '/api/sent-pings': {
        target: 'http://localhost:3001',
        changeOrigin: true,
      },
      '/api/disliked-profiles': {
        target: 'http://localhost:3001',
        changeOrigin: true,
      },
      '/api/discovered-profiles': {
        target: 'http://localhost:3001',
        changeOrigin: true,
      },
      '/api/location-rotation': {
        target: 'http://localhost:3001',
        changeOrigin: true,
      },
      '/api/saved-locations': {
        target: 'http://localhost:3001',
        changeOrigin: true,
      },
      '/api/data': {
        target: 'http://localhost:3001',
        changeOrigin: true,
      },
      '/api/auth': {
        target: 'http://localhost:3001',
        changeOrigin: true,
      },
      '/api/health': {
        target: 'http://localhost:3001',
        changeOrigin: true,
      },
    },
  },
})
