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

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    react(),
    VitePWA({
      registerType: 'autoUpdate',
      includeAssets: ['logo.png', 'logo2.png', 'favicon.ico'],
      manifest: {
        name: '萌芽监控面板',
        short_name: '萌芽监控',
        description: '服务器监控面板 - 萌芽监控',
        theme_color: '#1a1a2e',
        background_color: '#16213e',
        display: 'standalone',
        orientation: 'portrait-primary',
        scope: '/',
        start_url: '/',
        icons: [
          { src: '/logo.png', sizes: '512x512', type: 'image/png', purpose: 'any' },
          { src: '/logo2.png', sizes: '512x512', type: 'image/png', purpose: 'any maskable' },
        ],
        categories: ['utilities', 'productivity']
      },
      workbox: {
        globPatterns: ['**/*.{js,css,html,ico,svg,woff2}'],
        // 避免开发/生产 SPA 回退误吞 /central-api 转发
        navigateFallbackDenylist: [/^\/central-api(?:\/|$)/],
        runtimeCaching: [
          {
            urlPattern: /^https:\/\/.*\/api\//i,
            handler: 'NetworkFirst',
            options: {
              cacheName: 'api-cache',
              expiration: { maxEntries: 32, maxAgeSeconds: 60 * 5 },
              networkTimeoutSeconds: 10,
              cacheableResponse: { statuses: [0, 200] }
            }
          }
        ]
      },
      devOptions: {
        enabled: true
      }
    })
  ],
  server: {
    port: 2929,
    host: true,
    proxy: {
      '/central-api': {
        target: 'http://127.0.0.1:9393',
        changeOrigin: true,
        ws: true,
        rewrite: (path) => path.replace(/^\/central-api/, ''),
      },
    },
  },
})
