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

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

const app = await alchemy("kk");

/** Throws at deploy time if a required variable is missing from the environment. */
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: {
		// Core
		DATABASE_URL: getEnvVar("DATABASE_URL"),
		CORS_ORIGIN: getEnvVar("CORS_ORIGIN"),
		BETTER_AUTH_SECRET: getEnvVar("BETTER_AUTH_SECRET"),
		BETTER_AUTH_URL: getEnvVar("BETTER_AUTH_URL"),
		// Email (SMTP)
		SMTP_HOST: getEnvVar("SMTP_HOST"),
		SMTP_PORT: getEnvVar("SMTP_PORT"),
		SMTP_USER: getEnvVar("SMTP_USER"),
		SMTP_PASS: getEnvVar("SMTP_PASS"),
		SMTP_FROM: getEnvVar("SMTP_FROM"),
		// Payments (LemonSqueezy)
		LEMON_SQUEEZY_API_KEY: getEnvVar("LEMON_SQUEEZY_API_KEY"),
		LEMON_SQUEEZY_STORE_ID: getEnvVar("LEMON_SQUEEZY_STORE_ID"),
		LEMON_SQUEEZY_VARIANT_ID: getEnvVar("LEMON_SQUEEZY_VARIANT_ID"),
		LEMON_SQUEEZY_WEBHOOK_SECRET: getEnvVar("LEMON_SQUEEZY_WEBHOOK_SECRET"),
	},
	domains: ["kunstenkamp.be", "www.kunstenkamp.be"],
});

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

await app.finalize();
