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

export default defineConfig({
  plugins: [
    react(),
    VitePWA({
      registerType: 'autoUpdate',
      includeAssets: ['favicon.svg', 'models/**/*'],
      manifest: {
        name: 'cruciverb',
        short_name: 'cruciverb',
        description: 'a crossword a day',
        theme_color: '#111111',
        background_color: '#111111',
        display: 'standalone',
        start_url: '/',
        icons: [
          { src: '/favicon.svg', sizes: 'any', type: 'image/svg+xml', purpose: 'any maskable' },
        ],
      },
      workbox: {
        globPatterns: ['**/*.{js,css,html,svg,woff2,ttf}'],
        runtimeCaching: [
          {
            urlPattern: /^\/api\/puzzles\/daily\/.*/,
            handler: 'NetworkFirst',
            options: {
              cacheName: 'daily-puzzles',
              expiration: { maxEntries: 20, maxAgeSeconds: 86400 },
              networkTimeoutSeconds: 5,
            },
          },
          {
            urlPattern: /^\/api\/me$/,
            handler: 'NetworkFirst',
            options: {
              cacheName: 'session',
              expiration: { maxEntries: 1, maxAgeSeconds: 3600 },
              networkTimeoutSeconds: 3,
            },
          },
          {
            urlPattern: /^\/api\/.*/,
            handler: 'NetworkOnly',
          },
          {
            urlPattern: /^\/feeds\/.*/,
            handler: 'NetworkFirst',
            options: {
              cacheName: 'feeds',
              expiration: { maxEntries: 5, maxAgeSeconds: 3600 },
            },
          },
        ],
      },
    }),
  ],
  server: {
    port: 5173,
    proxy: {
      '/api': { target: 'http://localhost:8090', ws: true },
      '/feeds': { target: 'http://localhost:8090' },
      '/.well-known': { target: 'http://localhost:8090' },
      '/nodeinfo': { target: 'http://localhost:8090' },
      '/inbox': { target: 'http://localhost:8090' },
    },
  },
})
