import { fileURLToPath, URL } from 'node:url'
import Vue from '@vitejs/plugin-vue'
import Fonts from 'unplugin-fonts/vite'
import { defineConfig } from 'vite'
import Vuetify, { transformAssetUrls } from 'vite-plugin-vuetify'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    Vue({
      template: { transformAssetUrls },
    }),
    Vuetify({
      autoImport: true,
      styles: {
        configFile: 'src/styles/settings.scss',
      },
    }),
    Fonts({
      fontsource: {
        families: [
          {
            name: 'Roboto',
            weights: [100, 300, 400, 500, 700, 900],
            styles: ['normal', 'italic'],
          },
        ],
      },
    }),
  ],
  define: { 'process.env': {} },
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('src', import.meta.url)),
    },
    extensions: [
      '.js',
      '.json',
      '.jsx',
      '.mjs',
      '.ts',
      '.tsx',
      '.vue',
    ],
  },
  // Production build: output directly to web/app/ so the backend's StaticFiles
  // mount at /app/ serves the SPA without a copy step.
  // Old HTML pages (login.html etc.) coexist but are no longer the primary entry.
  base: '/app/',
  build: {
    outDir: fileURLToPath(new URL('../web/app', import.meta.url)),
    emptyOutDir: false, // Don't delete old HTML pages still in git
  },
  server: {
    port: 3000,
    proxy: {
      '/api': {
        target: 'https://api.synaglobal.vip',
        changeOrigin: true,
        secure: true,
      },
      '/ws': {
        target: 'wss://api.synaglobal.vip',
        ws: true,
        changeOrigin: true,
        secure: true,
      },
    },
  },
})
