import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import { resolve } from 'path';

// task-116b: this package now owns the Protera public web assets (moved out of
// themes/Web/Protera/Resources/assets/ in Step 1). They build here in isolation
// and publish to public/vendor/va-themes/protera/build/, from where the host
// emits them per-route via the @assets() Blade directive.
//
// Build output (npm run build → public/build/assets/), predictable (non-hashed)
// names so @assets() can reference them literally:
//   web.js  + web.css  — web.js imports its CSS (web.css) plus, transitively,
//                        search-suggest.js (+ search-suggest.css), header-search.js
//                        (+ header-search.css) and grid-frontend.js
//                        (+ grid-frontend.css). All of that lands in this entry's
//                        single CSS chunk → build/assets/web.css.
//   catalog-browsing.js + catalog-browsing.css — likewise, the JS imports its CSS.
//
// ⚠️ CSS files are NOT listed as standalone inputs: each .css is imported by its
// sibling .js. A standalone css/web.css + js/web.js pair shares the "web" basename
// and rollup (with non-hashed [name] output) renames the JS to web2.js — verified
// empirically during this task, and the same gotcha va-themes/viartas-default's
// vite.config documents. Importing the CSS from the JS keeps one basename per
// entry. This mirrors the legacy root @vite() loading (web.css + catalog-browsing
// .css + web.js + catalog-browsing.js); search-suggest rides along inside web.js.
export default defineConfig({
    // The built bundle is published to public/vendor/va-themes/protera/build/, so
    // any asset URL Vite rewrites inside the CSS (e.g. web.css's
    // `url('../images/hero-bg.png')`) must resolve from there. Without this base
    // the url() would point at `/build/assets/hero-bg.png` and 404 once published.
    base: '/vendor/va-themes/protera/build/',
    plugins: [laravel({
        input: [
            'Resources/assets/js/web.js',
            'Resources/assets/js/catalog-browsing.js',
            // SystemFindings/task-15 — three stylesheets lifted out of inline
            // <style> blocks. STANDALONE CSS entries are safe here (unlike the
            // web.css/web.js pair the note above warns about) because none of
            // these basenames collides with a JS entry.
            //
            // Not @import'ed into web.css on purpose: auth.css restyles bare
            // `body` and web.css loads on every public route, and the two
            // section sheets must load only when the operator actually placed
            // that section (the blades link them from their own `@once`).
            'Resources/assets/css/auth.css',
            'Resources/assets/css/section-divider.css',
            'Resources/assets/css/section-manifesto-pullquote.css',
        ],
        buildDirectory: 'build',
        hotFile: 'public/hot',
        publicDirectory: 'public',
        refresh: false,
    })],
    resolve: {
        alias: {
            // web.js pulls migrated module assets (HeaderSearch Alpine component,
            // GridBuilder frontend renderer) via the @va-modules alias so they
            // stay bundled in this theme build and share its Alpine instance.
            // va-modules sits two levels up from this package
            // (packages/va-themes/protera → packages/va-modules).
            '@va-modules': resolve(__dirname, '../../va-modules'),
            '@va-core':    resolve(__dirname, '../../va-core'),
            // '@web' mirrors the legacy root vite alias (themes/Web/Protera/
            // Resources/assets) — now the package's own Resources/assets.
            '@web':        resolve(__dirname, 'Resources/assets'),
        },
    },
    build: {
        outDir: 'public/build',
        emptyOutDir: true,
        manifest: true,
        rollupOptions: {
            output: {
                entryFileNames: 'assets/[name].js',
                chunkFileNames: 'assets/[name].js',
                assetFileNames: 'assets/[name].[ext]',
            },
        },
    },
});
