import { f as defineAdminEventHandler, aA as requireSuperAdmin, O as requireMfaStepUp, q as readBody, c as createError, aB as isValidUsername, aC as isValidEmail, 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 register_post = defineAdminEventHandler("admin:auth:register", async (event) => {
  try {
    await requireSuperAdmin(event);
    await requireMfaStepUp(event);
    const body = await readBody(event);
    const { username, email, password, firstName, lastName, role } = body;
    if (!username || !email || !password) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "Username, email, and password are required"
      });
    }
    if (!isValidUsername(username)) {
      throw createError({
        statusCode: 400,
        statusMessage: "Validation Error",
        message: "Username must be 3-50 characters (letters, numbers, underscore)"
      });
    }
    if (!isValidEmail(email)) {
      throw createError({
        statusCode: 400,
        statusMessage: "Validation Error",
        message: "Invalid email format"
      });
    }
    const passwordError = validatePassword(password);
    if (passwordError) {
      throw createError({
        statusCode: 400,
        statusMessage: "Validation Error",
        message: passwordError
      });
    }
    if (role) {
      const roleManager = createContentManager("role", { enforcePermissions: false });
      const found = await roleManager.findMany({ filters: { name: role }, limit: 1 });
      if (found.data.length === 0) {
        throw createError({
          statusCode: 400,
          statusMessage: "Validation Error",
          message: `Invalid role: '${role}'. Role does not exist.`
        });
      }
    }
    const adminUser = await createAdminUser({
      username: username.trim(),
      email: email.trim().toLowerCase(),
      password,
      firstName: typeof firstName === "string" ? firstName.trim() : void 0,
      lastName: typeof lastName === "string" ? lastName.trim() : void 0,
      role: role || EDITOR_ROLE.name
    });
    return {
      success: true,
      data: {
        user: {
          id: adminUser.id,
          username: adminUser.username,
          email: adminUser.email,
          firstName: adminUser.firstName,
          lastName: adminUser.lastName,
          role: adminUser.role
        }
      },
      message: "Admin user created successfully"
    };
  } catch (error) {
    if (error && typeof error === "object" && "statusCode" in error) {
      throw error;
    }
    if (error instanceof Error && (error.message.includes("already taken") || error.message.includes("already in use"))) {
      throw createError({
        statusCode: 409,
        statusMessage: "Conflict",
        message: error.message
      });
    }
    console.error("[POST /api/admin/auth/register] Error:", error);
    throw createError({
      statusCode: 500,
      statusMessage: "Registration Failed",
      message: error instanceof Error ? error.message : "Unknown error"
    });
  }
});

export { register_post as default };
//# sourceMappingURL=register.post.mjs.map
