import { d as defineEventHandler, r as requireAuth, cI as requireAdminScope, q as readBody, c as createError, a as getSchemaManager, br as getComponentRegistry, bs as SchemaValidationError, b as getDatabaseManager, ez as emitSchemaSync, eQ as emitSchemaCreate, eL as createSchemaRevision } from '../../nitro/nitro.mjs';
import { c as collectComponentRefsFromSchema } from '../../_/component-usage.mjs';
import { g as getAuditActorFromEvent } from '../../_/audit-log-actor.mjs';
import { i as invalidateGraphQLSchema } from '../../_/schema-builder.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 'graphql';

const index_post = defineEventHandler(async (event) => {
  var _a, _b, _c;
  try {
    await requireAuth(event);
    await requireAdminScope(event, "admin:schemas:write");
    const body = await readBody(event);
    if (!body) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "Request body is required"
      });
    }
    const manager = getSchemaManager();
    const componentRegistry = getComponentRegistry();
    const componentRefs = collectComponentRefsFromSchema(body);
    const missingRefs = componentRefs.filter((ref) => !componentRegistry.has(ref.component));
    if (missingRefs.length > 0) {
      throw createError({
        statusCode: 400,
        statusMessage: "Validation Error",
        message: "Schema references missing components",
        data: missingRefs
      });
    }
    const existingSchema = manager.getSchema((_a = body.info) == null ? void 0 : _a.singularName);
    if (existingSchema) {
      throw createError({
        statusCode: 409,
        statusMessage: "Conflict",
        message: `Schema '${body.info.singularName}' already exists`
      });
    }
    try {
      manager.saveSchema(body);
      invalidateGraphQLSchema();
    } catch (error) {
      if (error instanceof SchemaValidationError) {
        throw createError({
          statusCode: 400,
          statusMessage: "Validation Error",
          message: "Schema validation failed",
          data: error.result.errors
        });
      }
      throw error;
    }
    try {
      const dbManager = getDatabaseManager();
      await dbManager.syncSchemas();
      const schemaNames = manager.getAllSchemas().map((s) => s.info.singularName);
      await emitSchemaSync(schemaNames);
    } catch (syncError) {
      console.error("[POST /api/schemas] Database sync error:", syncError);
    }
    await emitSchemaCreate(body.info.singularName, body, getAuditActorFromEvent(event));
    try {
      await createSchemaRevision(
        body.info.singularName,
        "create",
        body,
        (_c = (_b = event.context.adminUser) == null ? void 0 : _b.id) != null ? _c : null
      );
    } catch (revisionError) {
      console.error("[POST /api/schemas] Failed to record schema revision:", revisionError);
    }
    return {
      success: true,
      message: "Schema created successfully",
      data: body
    };
  } catch (error) {
    if (error && typeof error === "object" && "statusCode" in error) {
      throw error;
    }
    if (error instanceof Error && error.message.includes("Authentication required")) {
      throw createError({
        statusCode: 401,
        statusMessage: "Unauthorized",
        message: error.message
      });
    }
    console.error("[POST /api/schemas] Error:", error);
    throw createError({
      statusCode: 500,
      statusMessage: "Failed to create schema",
      message: error instanceof Error ? error.message : "Unknown error"
    });
  }
});

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