import { defineConfig, loadEnv } from 'vite';
import laravel from 'laravel-vite-plugin';
import react from '@vitejs/plugin-react';
import tailwindcss from '@tailwindcss/vite';

export default defineConfig(({ command, mode }) => {
    const env = loadEnv(mode, process.cwd(), '');
    // base must point to the build output directory (always ends with /build/)
    const base = command === 'build' ? (env.VITE_BASE_URL || '/build/') : '/';
    // assetUrl strips /build/ suffix — used for public/ static files (images, etc.)
    const assetUrl = base.replace(/build\/$/, '');
    // storageUrl — base URL for Laravel storage files (uploaded via admin app)
    const storageUrl = (env.VITE_STORAGE_URL || assetUrl.replace(/\/$/, '')) + '/storage/';

    return {
        base,
        define: { __ASSET_URL__: JSON.stringify(assetUrl), __STORAGE_URL__: JSON.stringify(storageUrl) },
        server: {
            host: '0.0.0.0',
            port: 5173,
            hmr: {
                host: '192.168.0.213',
            },
        },
        plugins: [
            laravel({
                input: ['resources/css/app.css', 'resources/js/app.jsx'],
                refresh: true,
            }),
            react(),
            tailwindcss(),
        ],
    };
});
