import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import vue from '@vitejs/plugin-vue';
import vuetify from 'vite-plugin-vuetify';
import { visualizer } from 'rollup-plugin-visualizer';
import { resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { devIndicesPlugin } from './vite-plugins/dev-indices/index.ts';

const __dirname = fileURLToPath(new URL('.', import.meta.url));

// Vue 3 + Vuetify 3 (фаза 2 из CLAUDE.md §3.3).
// vite-plugin-vuetify авто-импортирует только используемые компоненты Vuetify
// (tree-shaking) — без ручных import'ов в каждом .vue.
export default defineConfig({
    plugins: [
        devIndicesPlugin({
            manifestPath: resolve(__dirname, 'dev-indices.json'),
            appRoot: __dirname,
            enabled:
                process.env.NODE_ENV !== 'production' &&
                process.env.VITE_DEV_INDICES !== '0' &&
                !process.env.VITEST, // CRITICAL: don't run during Vitest — would inject data-dx into test snapshots/queries
        }),
        laravel({
            input: ['resources/css/app.css', 'resources/js/app.ts'],
            refresh: true,
        }),
        vue({
            template: {
                transformAssetUrls: {
                    base: null,
                    includeAbsolute: false,
                },
            },
        }),
        vuetify({ autoImport: true }),
        // Bundle analyzer (Sprint 4 Phase C / audit O-refactor-06):
        // активен только при BUILD_ANALYZE=1. Генерирует storage/bundle-analyze.html.
        process.env.BUILD_ANALYZE === '1' && visualizer({
            filename: 'storage/bundle-analyze.html',
            open: false,
            gzipSize: true,
            brotliSize: true,
        }),
    ].filter(Boolean),
    server: {
        watch: {
            ignored: ['**/storage/framework/views/**'],
        },
    },
});
