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

// hosts allowed through Vite's host check (for tunnels: tunnelmole, localtunnel, cloudflare)
const ALLOWED_TUNNEL_HOSTS = ['.tunnelmole.net', '.loca.lt', '.trycloudflare.com']
const CMS_TARGET = process.env.VITE_CMS_TARGET || 'http://localhost:3001'

export default defineConfig({
  plugins: [react(), tailwindcss()],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),
    },
  },
  server: {
    allowedHosts: ALLOWED_TUNNEL_HOSTS,
    proxy: {
      '/api': {
        target: CMS_TARGET,
        changeOrigin: true,
        secure: true,
      },
      '/uploads': {
        target: CMS_TARGET,
        changeOrigin: true,
        secure: true,
      },
      '/media': {
        target: CMS_TARGET,
        changeOrigin: true,
        secure: true,
      },
    },
  },
  preview: {
    allowedHosts: ALLOWED_TUNNEL_HOSTS,
  },
})
