import { f as defineAdminEventHandler, h as requireAdminRole, q as readBody, c as createError, z as withAIGuardrails, B as approxBodyBytes, a as getSchemaManager, H as ComponentRegistry, C as getEnabledContentLocales, N as generateSchemaSuggestions } 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 schemaAssistant_post = defineAdminEventHandler("admin:ai:schema-assistant", async (event) => {
  var _a, _b, _c;
  await requireAdminRole(event, ["super-admin", "admin"]);
  const body = (_a = await readBody(event)) != null ? _a : {};
  const draft = parseDraft(body.draft);
  if (!draft) {
    throw createError({
      statusCode: 400,
      statusMessage: "Bad Request",
      message: 'Body must include "draft" object describing the schema being designed.'
    });
  }
  const intent = typeof body.intent === "string" ? body.intent : void 0;
  const request = { intent, draft };
  const data = await withAIGuardrails({
    advisor: "schema",
    event,
    bodyBytes: approxBodyBytes(body),
    inputDigest: {
      intent: intent != null ? intent : null,
      singularName: (_b = draft.singularName) != null ? _b : null,
      attributes: Object.keys((_c = draft.attributes) != null ? _c : {}).length
    },
    run: async () => {
      const schemaManager = getSchemaManager();
      const schemas = schemaManager.getAllSchemas();
      const components = ComponentRegistry.getAll();
      const enabledLocales = await getEnabledContentLocales();
      const context = {
        schemas,
        components,
        locales: enabledLocales.map((l) => l.code),
        i18nEnabled: enabledLocales.length > 1
      };
      return generateSchemaSuggestions(request, context);
    },
    digestOutput: (result) => ({
      suggestions: result.suggestions.length,
      byTrust: result.suggestions.reduce((acc, s) => {
        var _a2;
        acc[s.confidence] = ((_a2 = acc[s.confidence]) != null ? _a2 : 0) + 1;
        return acc;
      }, {})
    })
  });
  return { success: true, data };
});
function parseDraft(raw) {
  if (!raw || typeof raw !== "object") {
    return null;
  }
  const value = raw;
  const draft = {};
  if (value.kind === "collectionType" || value.kind === "singleType") {
    draft.kind = value.kind;
  }
  if (typeof value.singularName === "string") {
    draft.singularName = value.singularName;
  }
  if (typeof value.pluralName === "string") {
    draft.pluralName = value.pluralName;
  }
  if (typeof value.displayName === "string") {
    draft.displayName = value.displayName;
  }
  if (typeof value.description === "string") {
    draft.description = value.description;
  }
  if (value.attributes && typeof value.attributes === "object" && !Array.isArray(value.attributes)) {
    draft.attributes = value.attributes;
  }
  if (value.options && typeof value.options === "object" && !Array.isArray(value.options)) {
    draft.options = value.options;
  }
  return draft;
}

export { schemaAssistant_post as default };
//# sourceMappingURL=schema-assistant.post.mjs.map
