import { fileURLToPath, URL } from 'node:url';

import { PrimeVueResolver } from '@primevue/auto-import-resolver';
import tailwindcss from '@tailwindcss/vite';
import vue from '@vitejs/plugin-vue';
import { visualizer } from 'rollup-plugin-visualizer'; // bundle analyzer
import AutoImport from 'unplugin-auto-import/vite'; // auto import Vue composables
import Components from 'unplugin-vue-components/vite';
import viteCompression from 'vite-plugin-compression'; // 🔥 compressão gzip/brotli
import { defineConfig } from 'vitest/config';

// https://vitejs.dev/config/
export default defineConfig({
    test: {
        environment: 'node',
        include: ['src/**/*.spec.js'],
        alias: {
            '@': fileURLToPath(new URL('./src', import.meta.url))
        }
    },

    plugins: [
        vue(),

        tailwindcss(),

        // Auto import de APIs do Vue (ref, computed, onMounted, etc)
        AutoImport({
            imports: ['vue'],
            dts: 'src/auto-imports.d.ts' // gera tipagem automática
        }),

        // Auto import de componentes do PrimeVue
        Components({
            resolvers: [PrimeVueResolver()]
        }),

        // Visualizador de bundle (gera stats.html no build)
        visualizer({
            filename: 'dist/stats-before.html',
            open: true,
            gzipSize: true,
            brotliSize: true
        }),

        // 🔥 Gzip e Brotli
        viteCompression({ algorithm: 'gzip', ext: '.gz', threshold: 1024 }),
        viteCompression({ algorithm: 'brotliCompress', ext: '.br', threshold: 1024 })
    ],

    resolve: {
        alias: {
            '@': fileURLToPath(new URL('./src', import.meta.url))
        }
    },

    css: {
        preprocessorOptions: {
            scss: {
                api: 'modern-compiler'
            }
        }
    },

    optimizeDeps: {
        include: ['pdfmake/build/pdfmake', 'pdfmake/build/vfs_fonts']
    },

    build: {
        rollupOptions: {
            output: {
                manualChunks: {
                    vue: ['vue'],
                    primevue: ['primevue'],
                    chart: ['chart.js'],
                    supabase: ['@supabase/supabase-js'],
                    editor: ['quill']
                }
            }
        }
    }
});
