import { i as runMigrations } from "../../../../runner-K8XK101B.mjs";
import "../../../../dialect-helpers-DRI5pyY3.mjs";
import "../../../../base64-CmWvODNW.mjs";
import "../../../../types-CuW0zWDA.mjs";
import { n as apiSuccess, r as handleError, t as apiError } from "../../../../error-CkAseSqZ.mjs";
import { t as escapeHtml } from "../../../../escape-DqD0aCKu.mjs";
import { t as isSafeRedirect } from "../../../../redirect-CoW3G-0U.mjs";
import { ulid } from "ulidx";

//#region src/astro/routes/api/auth/dev-bypass.ts
const prerender = false;
const DEV_USER_EMAIL = "dev@emdash.local";
const DEV_USER_NAME = "Dev Admin";
const ROLE_ADMIN = 50;
async function handleDevBypass(context) {
	if (!import.meta.env.DEV) return apiError("FORBIDDEN", "Dev bypass is only available in development mode", 403);
	const { locals, url, session } = context;
	const { emdash } = locals;
	if (!emdash?.db) return apiError("NOT_CONFIGURED", "EmDash is not initialized", 500);
	try {
		await runMigrations(emdash.db);
		const existingUser = await emdash.db.selectFrom("users").selectAll().where("email", "=", DEV_USER_EMAIL).executeTakeFirst();
		let user;
		if (!existingUser) {
			const now = (/* @__PURE__ */ new Date()).toISOString();
			const newUser = {
				id: ulid(),
				email: DEV_USER_EMAIL,
				name: DEV_USER_NAME,
				role: ROLE_ADMIN,
				email_verified: 1,
				created_at: now,
				updated_at: now
			};
			await emdash.db.insertInto("users").values(newUser).execute();
			user = {
				id: newUser.id,
				email: newUser.email,
				name: newUser.name,
				role: newUser.role
			};
			console.log("[dev-bypass] Created dev admin user:", user.email);
		} else user = {
			id: existingUser.id,
			email: existingUser.email,
			name: existingUser.name || DEV_USER_NAME,
			role: existingUser.role
		};
		if (session) session.set("user", { id: user.id });
		const redirect = url.searchParams.get("redirect");
		if (redirect) {
			if (!isSafeRedirect(redirect)) return apiError("INVALID_REDIRECT", "Redirect must be a local path", 400);
			const html = `<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="refresh" content="0;url=${escapeHtml(redirect)}">
</head>
<body>Redirecting...</body>
</html>`;
			return new Response(html, {
				status: 200,
				headers: { "Content-Type": "text/html" }
			});
		}
		return apiSuccess({
			success: true,
			message: "Dev session created",
			user: {
				id: user.id,
				email: user.email,
				name: user.name,
				role: user.role
			}
		});
	} catch (error) {
		return handleError(error, "Dev bypass setup failed", "DEV_BYPASS_ERROR");
	}
}
const GET = handleDevBypass;
const POST = handleDevBypass;

//#endregion
export { GET, POST, prerender };
//# sourceMappingURL=dev-bypass.mjs.map