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 (Mollie)
		MOLLIE_API_KEY: getEnvVar("MOLLIE_API_KEY"),
		// Cron secret for protected scheduled endpoints
		CRON_SECRET: getEnvVar("CRON_SECRET"),
	},
	// Fire every hour so the reminder check can run at 09:30 on 2026-03-11
	crons: ["0 * * * *"],
	domains: ["kunstenkamp.be", "www.kunstenkamp.be"],
});

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

await app.finalize();
