import "../../../../../../base64-CmWvODNW.mjs";
import "../../../../../../types-CuW0zWDA.mjs";
import { t as withTransaction } from "../../../../../../transaction-qfqpPVpu.mjs";
import { n as apiSuccess, r as handleError, t as apiError } from "../../../../../../error-CkAseSqZ.mjs";
import { createKyselyAdapter } from "@emdash-cms/auth/adapters/kysely";
import { Role } from "@emdash-cms/auth";

//#region src/astro/routes/api/admin/users/[id]/disable.ts
/**
* User disable endpoint
*
* POST /_emdash/api/admin/users/:id/disable - Soft-disable a user
*/
const prerender = false;
const POST = async ({ params, locals }) => {
	const { emdash, user: currentUser } = locals;
	if (!emdash?.db) return apiError("NOT_CONFIGURED", "Database not configured", 500);
	if (!currentUser || currentUser.role < Role.ADMIN) return apiError("FORBIDDEN", "Admin privileges required", 403);
	const adapter = createKyselyAdapter(emdash.db);
	const { id } = params;
	if (!id) return apiError("VALIDATION_ERROR", "User ID required", 400);
	if (id === currentUser.id) return apiError("VALIDATION_ERROR", "Cannot disable your own account", 400);
	try {
		const targetUser = await adapter.getUserById(id);
		if (!targetUser) return apiError("NOT_FOUND", "User not found", 404);
		if (await withTransaction(emdash.db, async (trx) => {
			const trxAdapter = createKyselyAdapter(trx);
			if (targetUser.role === Role.ADMIN) {
				if (await trxAdapter.countAdmins() <= 1) return true;
			}
			await trxAdapter.updateUser(id, { disabled: true });
			return false;
		})) return apiError("VALIDATION_ERROR", "Cannot disable the last admin. Promote another user first.", 400);
		await emdash.db.deleteFrom("_emdash_oauth_tokens").where("user_id", "=", id).execute();
		return apiSuccess({ success: true });
	} catch (error) {
		return handleError(error, "Failed to disable user", "USER_DISABLE_ERROR");
	}
};

//#endregion
export { POST, prerender };
//# sourceMappingURL=disable.mjs.map