import "../../../../../../base64-CmWvODNW.mjs";
import "../../../../../../types-CuW0zWDA.mjs";
import { t as OptionsRepository } from "../../../../../../options-DTTML-Tx.mjs";
import { n as apiSuccess, r as handleError, t as apiError } from "../../../../../../error-CkAseSqZ.mjs";
import { t as getSiteBaseUrl } from "../../../../../../site-url-BBfDTgZB.mjs";
import { createKyselyAdapter } from "@emdash-cms/auth/adapters/kysely";
import { Role, sendMagicLink } from "@emdash-cms/auth";

//#region src/astro/routes/api/admin/users/[id]/send-recovery.ts
/**
* Send recovery link endpoint
*
* POST /_emdash/api/admin/users/:id/send-recovery
*
* Admin-initiated account recovery — sends a recovery magic link to the user's email.
*/
const prerender = false;
const POST = async ({ request, params, locals }) => {
	const { emdash, user } = locals;
	if (!emdash?.db) return apiError("NOT_CONFIGURED", "Database not configured", 500);
	if (!user || user.role < Role.ADMIN) return apiError("FORBIDDEN", "Admin privileges required", 403);
	const { id } = params;
	if (!id) return apiError("VALIDATION_ERROR", "User ID required", 400);
	try {
		const adapter = createKyselyAdapter(emdash.db);
		const targetUser = await adapter.getUserById(id);
		if (!targetUser) return apiError("NOT_FOUND", "User not found", 404);
		if (!emdash.email?.isAvailable()) return apiError("EMAIL_NOT_CONFIGURED", "Email is not configured. Recovery links require an email provider.", 503);
		const options = new OptionsRepository(emdash.db);
		await sendMagicLink({
			baseUrl: await getSiteBaseUrl(emdash.db, request),
			siteName: await options.get("emdash:site_title") ?? "EmDash",
			email: (message) => emdash.email.send(message, "system")
		}, adapter, targetUser.email, "recovery");
		return apiSuccess({
			success: true,
			message: "Recovery link sent"
		});
	} catch (error) {
		return handleError(error, "Failed to send recovery link", "RECOVERY_SEND_ERROR");
	}
};

//#endregion
export { POST, prerender };
//# sourceMappingURL=send-recovery.mjs.map