import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import react from '@vitejs/plugin-react';
import tailwindcss from '@tailwindcss/vite';
import path from 'node:path';
import { readFileSync } from 'node:fs';

// Surface the project's version string (canonical source: top-level
// `VERSION` file; that's the same file `bin/build-release.sh` reads to
// name the dist ZIP) to the client at build time so the editor's About
// strip displays it. `define` inlines the value at every reference; we
// use a custom global (__APP_VERSION__) rather than overriding
// `import.meta.env` since Vite reserves the latter for its own .env-file
// resolution.
const APP_VERSION = (() => {
    try {
        return readFileSync(path.resolve(__dirname, 'VERSION'), 'utf-8').trim();
    } catch {
        return '0.0.0';
    }
})();

export default defineConfig({
    define: {
        __APP_VERSION__: JSON.stringify(APP_VERSION),
    },
    plugins: [
        laravel({
            input: ['resources/css/app.css', 'resources/js/app.tsx'],
            refresh: true,
        }),
        react(),
        tailwindcss(),
    ],
    resolve: {
        alias: {
            '@': path.resolve(__dirname, 'resources/js'),
        },
    },
    server: {
        // Bind to IPv4 only — Node 18+ prefers IPv6 [::1], but CSP source
        // lists can't express bracketed IPv6 hosts. Pinning to 127.0.0.1 keeps
        // the HMR origin CSP-friendly.
        host: '127.0.0.1',
        watch: {
            ignored: ['**/storage/framework/views/**'],
        },
    },
});
