import { paraglideVitePlugin } from '@inlang/paraglide-js';
import tailwindcss from '@tailwindcss/vite';
import { defineConfig } from 'vitest/config';
import { sveltekit } from '@sveltejs/kit/vite';

/* En CI, por defecto solo Node (evita Chromium). Con JENKINS_VITEST_FULL=1 o VITEST_FULL=1, también @vitest/browser. */
const inCi = process.env.CI === 'true' || Boolean(process.env.JENKINS_URL);
const vitestFull =
	process.env.VITEST_FULL === '1' || process.env.JENKINS_VITEST_FULL === '1';
const skipBrowser = process.env.VITEST_NO_BROWSER === '1';
const vitestNoBrowser = (inCi && !vitestFull) || skipBrowser;

const vitestServerProject = {
	extends: './vite.config.ts',
	test: {
		name: 'server' as const,
		environment: 'node' as const,
		include: ['src/**/*.{test,spec}.{js,ts}'],
		exclude: ['src/**/*.svelte.{test,spec}.{js,ts}'],
	},
};

const vitestClientProject = {
	extends: './vite.config.ts',
	test: {
		name: 'client' as const,
		environment: 'browser' as const,
		browser: {
			enabled: true,
			provider: 'playwright' as const,
			instances: [{ browser: 'chromium' as const }],
		},
		include: ['src/**/*.svelte.{test,spec}.{js,ts}'],
		exclude: ['src/lib/server/**'],
		setupFiles: ['./vitest-setup-client.ts'],
	},
};

export default defineConfig({
	server: {
		port: 	5173, // fija el puerto
		host: true, // escucha en 0.0.0.0
		// Lista explícita + peticiones internas (p. ej. chunks JSON `?import`) pueden usar
		// Hosts distintos y recibir 403. `true` permite cualquier Host en dev.
		allowedHosts: true,
		proxy: {
			'/api/uploads': {
				target: 'http://backend:8000',
				changeOrigin: true
			},
			'/api/v1/core/help-center': {
				target: 'http://backend:8000',
				changeOrigin: true
			}
		}
	},
	plugins: [
		tailwindcss(),
		sveltekit(),
		paraglideVitePlugin({
			project: './project.inlang',
			outdir: './src/lib/paraglide'
		})
	],
	test: {
		expect: { requireAssertions: true },
		projects: vitestNoBrowser
			? [vitestServerProject]
			: [vitestClientProject, vitestServerProject]
	}
});
