import { f as defineAdminEventHandler, aA as requireSuperAdmin, O as requireMfaStepUp, c as createError, q as readBody, aD as validatePassword, G as createContentManager, dh as updateAdminUser } 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 _id__put = defineAdminEventHandler("admin:users:update", async (event) => {
  var _a, _b, _c;
  try {
    await requireSuperAdmin(event);
    await requireMfaStepUp(event);
    const id = parseInt(((_a = event.context.params) == null ? void 0 : _a.id) || "", 10);
    if (isNaN(id)) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "Invalid user ID"
      });
    }
    const body = await readBody(event);
    if (body.email) {
      const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
      if (!emailRegex.test(body.email)) {
        throw createError({
          statusCode: 400,
          statusMessage: "Bad Request",
          message: "Invalid email format"
        });
      }
    }
    if (body.password) {
      const passwordError = validatePassword(body.password);
      if (passwordError) {
        throw createError({
          statusCode: 400,
          statusMessage: "Bad Request",
          message: passwordError
        });
      }
    }
    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 updateData = {};
    if (body.username) {
      updateData.username = body.username.trim();
    }
    if (body.email) {
      updateData.email = body.email.trim().toLowerCase();
    }
    if (body.password) {
      updateData.password = body.password;
    }
    if (body.firstName !== void 0) {
      updateData.firstName = (_b = body.firstName) == null ? void 0 : _b.trim();
    }
    if (body.lastName !== void 0) {
      updateData.lastName = (_c = body.lastName) == null ? void 0 : _c.trim();
    }
    if (body.role) {
      updateData.role = body.role;
    }
    if (body.isActive !== void 0) {
      updateData.isActive = body.isActive;
    }
    const user = await updateAdminUser(id, updateData);
    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
      });
    }
    if (errorMessage.includes("not found")) {
      throw createError({
        statusCode: 404,
        statusMessage: "Not Found",
        message: errorMessage
      });
    }
    if (errorMessage.includes("last admin owner")) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: errorMessage
      });
    }
    console.error("[PUT /api/admin/users/:id] Error:", error);
    throw createError({
      statusCode: 500,
      statusMessage: "Failed to update admin user",
      message: errorMessage
    });
  }
});

export { _id__put as default };
//# sourceMappingURL=_id_.put.mjs.map
