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

try {
  const svgPath = path.resolve(__dirname, '../logotipo_brainwind color original3.svg')
  if (fs.existsSync(svgPath)) {
    let text = fs.readFileSync(svgPath, 'utf8')
    text = text.replace(/([0-9]+\.[0-9]+)/g, (m) => parseFloat(m).toFixed(1).replace(/\.0$/, ''))
    text = text.replace(/\s+/g, ' ')
    fs.mkdirSync(path.resolve(__dirname, 'public'), { recursive: true })
    fs.writeFileSync(path.resolve(__dirname, 'public/logo_brainwind.svg'), text)
    fs.writeFileSync(path.resolve(__dirname, 'public/logo_brainwind_dark.svg'), text.replace(/#06315A/gi, '#E2E8F0'))
  }
  
  const pngPath = path.resolve(__dirname, '../logotipo_brainwind color original.png')
  if (fs.existsSync(pngPath)) {
    fs.mkdirSync(path.resolve(__dirname, 'public'), { recursive: true })
    fs.copyFileSync(pngPath, path.resolve(__dirname, 'public/logo_brainwind.png'))
  }
} catch(e) {}

// https://vite.dev/config/
export default defineConfig({
  plugins: [react(), tailwindcss()],
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"),
    },
  },
  server: {
    // Permite acesso via hostnames customizados em dev (Traefik/Coolify)
    allowedHosts: true,
  },
  preview: {
    // Permite acesso via hostnames customizados em preview (produção via Coolify)
    allowedHosts: true,
  },
})