import alchemy from "alchemy";
import { TanStackStart } from "alchemy/cloudflare";
import { config } from "dotenv";

config({ path: "./.env" });
config({ path: "../../apps/web/.env" });

const app = await alchemy("kk");

// Helper function to get required env var
function getEnvVar(name: string): string {
	const value = process.env[name];
	if (!value) {
		throw new Error(`Missing required environment variable: ${name}`);
	}
	return value;
}

export const web = await TanStackStart("web", {
	cwd: "../../apps/web",
	bindings: {
		DATABASE_URL: getEnvVar("DATABASE_URL"),
		CORS_ORIGIN: getEnvVar("CORS_ORIGIN"),
		BETTER_AUTH_SECRET: getEnvVar("BETTER_AUTH_SECRET"),
		BETTER_AUTH_URL: getEnvVar("BETTER_AUTH_URL"),
	},
});

console.log(`Web    -> ${web.url}`);

await app.finalize();
