import tailwindcss from "@tailwindcss/vite";
import { tanstackRouter } from "@tanstack/router-plugin/vite";
import react from "@vitejs/plugin-react-swc";
import dotenv from "dotenv";
import path from "path";
import { fileURLToPath } from "url";
import { defineConfig } from "vite";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

dotenv.config({
	path: path.resolve(__dirname, "../.env"),
});

// https://vite.dev/config/
export default defineConfig({
	plugins: [
		react(),
		tailwindcss(),
		tanstackRouter({
			target: "react",
			autoCodeSplitting: true,
		}),
	],
	base: "/lst/app",
	build: {
		//outDir: "../backend/frontend",
		//assetsDir: "assets",
		emptyOutDir: true,
	},
	resolve: {
		alias: {
			"@": path.resolve(__dirname, "./src"),
		},
	},
	server: {
		port: 5500,
		host: true,
		allowedHosts: true,
		proxy: {
			"/lst/api": {
				target: `http://localhost:${Number(process.env.VITE_PORT || 4000)}`,
				changeOrigin: true,
				secure: false,
			},
			"/lst/old": {
				target: `http://localhost:${Number(process.env.VITE_PORT || 4000)}`,
				changeOrigin: true,
				secure: false,
			},
			"/lst/ws": {
				target: `ws://localhost:${Number(process.env.VITE_PORT || 4000)}`, // Your Go WebSocket endpoint
				ws: true,
				changeOrigin: true,
				secure: false,
				rewrite: (path) => path.replace(/^\/ws/, ""),
			},
			// Proxy docs to Docusaurus
			"/lst/d": {
				target: `http://localhost:4300`,
				changeOrigin: true,
				//rewrite: (path) => path.replace(/^\/lst\/d/, "/"),
			},
			"/ocme": {
				target: `http://localhost:${Number(process.env.VITE_PORT || 4000)}`,
				changeOrigin: true,
				secure: false,
			},
		},
	},
});
