import { f as defineAdminEventHandler, h as requireAdminRole, O as requireMfaStepUp, q as readBody, c as createError, cV as createServiceAccount } from '../../../nitro/nitro.mjs';
import { a as auditFromEvent } from '../../../_/audit-log-helpers.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';
import '../../../_/audit-log-actor.mjs';

const index_post = defineAdminEventHandler("admin:service-accounts:create", async (event) => {
  var _a;
  await requireAdminRole(event, ["super-admin", "admin"]);
  await requireMfaStepUp(event);
  const body = await readBody(event);
  if (!body.name || typeof body.name !== "string" || body.name.trim().length === 0) {
    throw createError({
      statusCode: 400,
      statusMessage: "Bad Request",
      message: "Service account name is required"
    });
  }
  if (body.name.trim().length > 255) {
    throw createError({
      statusCode: 400,
      statusMessage: "Bad Request",
      message: "Service account name must be 255 characters or fewer"
    });
  }
  const adminUser = event.context.adminUser;
  if (!adminUser) {
    throw createError({
      statusCode: 401,
      statusMessage: "Unauthorized",
      message: "Admin user required"
    });
  }
  try {
    const created = await createServiceAccount(adminUser.id, {
      name: body.name.trim(),
      description: (_a = body.description) == null ? void 0 : _a.trim()
    });
    auditFromEvent(event, "service_account.create", {
      target: { entityType: "service_account", entityId: created.id, displayName: created.name }
    });
    return { success: true, data: created };
  } catch (error) {
    const errorMessage = error instanceof Error ? error.message : "Unknown error";
    if (errorMessage === "Service account name is already in use") {
      throw createError({
        statusCode: 409,
        statusMessage: "Conflict",
        message: errorMessage
      });
    }
    throw error;
  }
});

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