import { f as defineAdminEventHandler, h as requireAdminRole, O as requireMfaStepUp, c as createError, q as readBody, R as validateScopes, S as updateApiToken } 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 _id__put = defineAdminEventHandler("admin:api-tokens:update", async (event) => {
  var _a, _b, _c;
  try {
    await requireAdminRole(event, ["super-admin", "admin"]);
    await requireMfaStepUp(event);
    const id = Number((_a = event.context.params) == null ? void 0 : _a.id);
    if (!id || isNaN(id)) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "Invalid token ID"
      });
    }
    const body = await readBody(event);
    if (body.name !== void 0) {
      if (typeof body.name !== "string" || body.name.trim().length === 0) {
        throw createError({
          statusCode: 400,
          statusMessage: "Bad Request",
          message: "Token name cannot be empty"
        });
      }
      if (body.name.trim().length > 255) {
        throw createError({
          statusCode: 400,
          statusMessage: "Bad Request",
          message: "Token name must be 255 characters or fewer"
        });
      }
    }
    if (body.scopes !== void 0) {
      const scopeError = validateScopes(body.scopes);
      if (scopeError) {
        throw createError({
          statusCode: 400,
          statusMessage: "Bad Request",
          message: scopeError
        });
      }
    }
    if (body.expiresAt !== void 0 && body.expiresAt !== null) {
      const expiresDate = new Date(body.expiresAt);
      if (isNaN(expiresDate.getTime())) {
        throw createError({
          statusCode: 400,
          statusMessage: "Bad Request",
          message: "Invalid expiresAt date format"
        });
      }
    }
    if (body.serviceAccountId !== void 0 && body.serviceAccountId !== null) {
      if (!Number.isInteger(body.serviceAccountId) || body.serviceAccountId <= 0) {
        throw createError({
          statusCode: 400,
          statusMessage: "Bad Request",
          message: "serviceAccountId must be a positive integer or null"
        });
      }
    }
    const token = await updateApiToken(id, {
      name: (_b = body.name) == null ? void 0 : _b.trim(),
      description: (_c = body.description) == null ? void 0 : _c.trim(),
      scopes: body.scopes,
      expiresAt: body.expiresAt,
      isActive: body.isActive,
      serviceAccountId: body.serviceAccountId
    });
    auditFromEvent(event, "api_token.update", {
      target: { entityType: "api_token", entityId: token.id, displayName: token.name },
      metadata: {
        changedKeys: Object.keys(body),
        scopes: token.scopes,
        isActive: token.isActive
      }
    });
    return {
      success: true,
      data: token
    };
  } catch (error) {
    if (error && typeof error === "object" && "statusCode" in error) {
      throw error;
    }
    const errorMessage = error instanceof Error ? error.message : "Unknown error";
    if (errorMessage === "API token not found") {
      throw createError({
        statusCode: 404,
        statusMessage: "Not Found",
        message: errorMessage
      });
    }
    if (errorMessage === "Service account not found") {
      throw createError({
        statusCode: 404,
        statusMessage: "Not Found",
        message: errorMessage
      });
    }
    console.error("[PUT /api/admin/api-tokens/:id] Error:", error);
    throw createError({
      statusCode: 500,
      statusMessage: "Failed to update API token",
      message: errorMessage
    });
  }
});

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