import { f as defineAdminEventHandler, aA as requireSuperAdmin, O as requireMfaStepUp, q as readBody, c as createError, aD as validatePassword, G as createContentManager, aE as createAdminUser, aF as EDITOR_ROLE } from '../../../nitro/nitro.mjs';
import 'node:crypto';
import 'jsonwebtoken';
import 'node:fs';
import 'node:module';
import 'node:path';
import 'node:url';
import 'node:http';
import 'node:https';
import 'node:events';
import 'node:buffer';
import 'pino';
import 'drizzle-orm/better-sqlite3';
import 'drizzle-orm/node-postgres';
import 'drizzle-orm/mysql2';
import 'better-sqlite3';
import 'pg';
import 'mysql2/promise';
import 'drizzle-orm';
import 'fs';
import 'path';
import 'prom-client';
import 'bcryptjs';
import '@node-rs/argon2';
import 'node:async_hooks';
import 'crypto';
import 'node:dns';
import 'node:util';
import 'node:net';
import 'fs/promises';
import 'url';
import 'semver';

const index_post = defineAdminEventHandler("admin:users:create", async (event) => {
  var _a, _b;
  try {
    await requireSuperAdmin(event);
    await requireMfaStepUp(event);
    const body = await readBody(event);
    if (!body.username || typeof body.username !== "string") {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "Username is required"
      });
    }
    if (!body.email || typeof body.email !== "string") {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "Email is required"
      });
    }
    const passwordError = validatePassword(body.password || "");
    if (passwordError) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: passwordError
      });
    }
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    if (!emailRegex.test(body.email)) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "Invalid email format"
      });
    }
    if (body.role) {
      const roleManager = createContentManager("role", { enforcePermissions: false });
      const found = await roleManager.findMany({
        filters: { name: body.role },
        limit: 1
      });
      if (found.data.length === 0) {
        throw createError({
          statusCode: 400,
          statusMessage: "Bad Request",
          message: `Invalid role: '${body.role}'. Role does not exist.`
        });
      }
    }
    const user = await createAdminUser({
      username: body.username.trim(),
      email: body.email.trim().toLowerCase(),
      password: body.password,
      firstName: (_a = body.firstName) == null ? void 0 : _a.trim(),
      lastName: (_b = body.lastName) == null ? void 0 : _b.trim(),
      role: body.role || EDITOR_ROLE.name
    });
    const { password: _, ...userWithoutPassword } = user;
    return {
      success: true,
      data: userWithoutPassword
    };
  } catch (error) {
    if (error && typeof error === "object" && "statusCode" in error) {
      throw error;
    }
    const errorMessage = error instanceof Error ? error.message : "Unknown error";
    if (errorMessage.includes("already taken") || errorMessage.includes("already in use")) {
      throw createError({
        statusCode: 409,
        statusMessage: "Conflict",
        message: errorMessage
      });
    }
    console.error("[POST /api/admin/users] Error:", error);
    throw createError({
      statusCode: 500,
      statusMessage: "Failed to create admin user",
      message: errorMessage
    });
  }
});

export { index_post as default };
//# sourceMappingURL=index.post9.mjs.map
