import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import tailwindcss from '@tailwindcss/vite';

export default defineConfig({
    plugins: [
        tailwindcss(),
        laravel({
            input: [
                'resources/css/app.css',
                'resources/js/app.js',
            ],
            refresh: [
                'resources/views/**/*.blade.php',
                'app/View/Components/**/*.php',
                'app/Http/Controllers/**/*.php',
                'routes/**/*.php',
            ],
        }),
    ],
    resolve: {
        alias: {
            '@': '/resources/js',
        },
    },
    server: {
        hmr: {
            host: 'localhost',
        },
    },
    optimizeDeps: {
        include: [
            'chart.js',
            'leaflet',
            'pusher-js',
            'laravel-echo',
            'select2',
            'sweetalert2',
            'toastr',
            'dropzone',
            'flatpickr',
            'quill',
            '@fullcalendar/core',
            '@fullcalendar/daygrid',
            '@fullcalendar/timegrid',
            '@fullcalendar/list',
        ],
    },
    build: {
        chunkSizeWarningLimit: 600,
        rollupOptions: {
            output: {
                // Split vendor code into logical chunks so browsers can cache them independently
                manualChunks(id) {
                    // Alpine.js + Flowbite — UI framework core
                    if (id.includes('alpinejs') || id.includes('@alpinejs') || id.includes('flowbite')) {
                        return 'vendor-alpine';
                    }
                    // FontAwesome — icon fonts
                    if (id.includes('@fortawesome')) {
                        return 'vendor-fontawesome';
                    }
                    // Chart.js — loaded only on dashboard/report pages
                    if (id.includes('chart.js')) {
                        return 'vendor-charts';
                    }
                    // FullCalendar — loaded only on training/mentorship pages
                    if (id.includes('@fullcalendar')) {
                        return 'vendor-calendar';
                    }
                    // Leaflet — loaded only on health directory map pages
                    if (id.includes('leaflet')) {
                        return 'vendor-maps';
                    }
                    // Pusher + Laravel Echo — loaded only on chat pages
                    if (id.includes('pusher-js') || id.includes('laravel-echo')) {
                        return 'vendor-realtime';
                    }
                    // Quill rich text editor
                    if (id.includes('quill') || id.includes('parchment') || id.includes('delta')) {
                        return 'vendor-editor';
                    }
                    // DataTables
                    if (id.includes('datatables.net')) {
                        return 'vendor-datatables';
                    }
                    // jQuery (required by select2 and datatables)
                    if (id.includes('node_modules/jquery')) {
                        return 'vendor-jquery';
                    }
                    // Remaining node_modules go into a general vendor chunk
                    if (id.includes('node_modules')) {
                        return 'vendor';
                    }
                },
                assetFileNames: (assetInfo) => {
                    if (assetInfo.name && assetInfo.name.match(/\.(woff2?|eot|ttf|otf)$/)) {
                        return 'assets/webfonts/[name]-[hash][extname]';
                    }
                    if (assetInfo.name && assetInfo.name.match(/\.(png|gif|jpg|jpeg|svg)$/)) {
                        return 'assets/images/[name]-[hash][extname]';
                    }
                    return 'assets/[name]-[hash][extname]';
                },
            },
        },
        // Minification and optimization
        minify: 'esbuild',
        esbuild: {
            drop: ['console', 'debugger'], // Remove console.log and debugger in production
        },
        // CSS code splitting
        cssCodeSplit: true,
        // Source maps for production debugging (optional)
        sourcemap: false,
    },
});
