import { f as defineAdminEventHandler, h as requireAdminRole, a as getSchemaManager, bE as MAX_SEARCH_QUERY_LENGTH, bF as MAX_CROSS_SEARCH_CONTENT_TYPES, bG as MAX_SEARCH_FIELDS, bH as MAX_GROUP_BY_FIELDS, bI as MAX_AGGREGATE_FUNCTIONS, bJ as MAX_FILTER_CONDITIONS, bK as MAX_POPULATE_FIELDS, bL as MAX_QUERY_OFFSET, bM as MAX_QUERY_LIMIT, G as createContentManager, bN as dbQueryOne } from '../../../../nitro/nitro.mjs';
import { getSearchService } from '../../../../_/index.mjs';
import { g as getDeliveryMetrics } from '../../../../_/delivery-metrics.mjs';
import { g as getGraphQLSchema } 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';
import '../../../../_/audit-log-actor.mjs';

function errorMessage(error) {
  return error instanceof Error ? error.message : String(error);
}
function collectGraphQL() {
  try {
    const schema = getGraphQLSchema();
    const queryType = schema.getQueryType();
    const mutationType = schema.getMutationType();
    const subscriptionType = schema.getSubscriptionType();
    const typeMap = schema.getTypeMap();
    const userTypes = Object.keys(typeMap).filter((name) => !name.startsWith("__"));
    return {
      enabled: true,
      path: "/api/graphql",
      playgroundPath: "/api/graphql",
      introspection: {
        queryFields: queryType ? Object.keys(queryType.getFields()).length : 0,
        mutationFields: mutationType ? Object.keys(mutationType.getFields()).length : 0,
        subscriptionFields: subscriptionType ? Object.keys(subscriptionType.getFields()).length : 0,
        typeCount: userTypes.length
      },
      error: null
    };
  } catch (error) {
    return {
      enabled: false,
      path: "/api/graphql",
      playgroundPath: "/api/graphql",
      introspection: null,
      error: errorMessage(error)
    };
  }
}
async function collectMediaIndex() {
  try {
    const result = await dbQueryOne(
      "SELECT COUNT(*) as count FROM media",
      []
    );
    const count = result ? Number(result.count) : 0;
    return {
      name: "media",
      displayName: "Media",
      kind: "media",
      documentCount: count,
      searchableFields: ["name", "alt", "caption", "mime"],
      error: null
    };
  } catch (error) {
    return {
      name: "media",
      displayName: "Media",
      kind: "media",
      documentCount: null,
      searchableFields: [],
      error: errorMessage(error)
    };
  }
}
async function collectContentIndex(singularName, displayName, kind) {
  var _a;
  try {
    const manager = createContentManager(singularName);
    const result = await manager.findMany({ limit: 1, offset: 0 });
    const schemaManager = getSchemaManager();
    const schema = schemaManager.getSchema(singularName);
    const searchableFields = schema ? Object.entries((_a = schema.attributes) != null ? _a : {}).filter(([, def]) => {
      const type = def.type;
      return type === "string" || type === "text" || type === "richtext";
    }).map(([key]) => key) : [];
    return {
      name: singularName,
      displayName,
      kind,
      documentCount: result.total,
      searchableFields,
      error: null
    };
  } catch (error) {
    return {
      name: singularName,
      displayName,
      kind,
      documentCount: null,
      searchableFields: [],
      error: errorMessage(error)
    };
  }
}
async function collectSearch() {
  try {
    const service = getSearchService();
    const adapter = service.getAdapter();
    const health = await service.healthCheck();
    const schemaManager = getSchemaManager();
    const schemas = schemaManager.getAllSchemas().filter(
      (schema) => schema.info.singularName !== "media" && !schemaManager.isHiddenSchema(schema.info.singularName)
    );
    const contentIndices = await Promise.all(
      schemas.map(
        (schema) => collectContentIndex(
          schema.info.singularName,
          schema.info.displayName,
          schema.kind === "singleType" ? "singleType" : "collectionType"
        )
      )
    );
    const mediaIndex = await collectMediaIndex();
    return {
      adapterName: adapter.name,
      capabilities: { ...adapter.capabilities },
      health: { healthy: health.healthy, message: health.message },
      indices: [mediaIndex, ...contentIndices],
      error: null
    };
  } catch (error) {
    return {
      adapterName: "unknown",
      capabilities: {},
      health: { healthy: false, message: errorMessage(error) },
      indices: [],
      error: errorMessage(error)
    };
  }
}
function collectGuardrails() {
  return {
    limits: {
      maxQueryLimit: MAX_QUERY_LIMIT,
      maxQueryOffset: MAX_QUERY_OFFSET,
      maxPopulateFields: MAX_POPULATE_FIELDS,
      maxFilterConditions: MAX_FILTER_CONDITIONS,
      maxAggregateFunctions: MAX_AGGREGATE_FUNCTIONS,
      maxGroupByFields: MAX_GROUP_BY_FIELDS,
      maxSearchFields: MAX_SEARCH_FIELDS,
      maxCrossSearchContentTypes: MAX_CROSS_SEARCH_CONTENT_TYPES,
      maxSearchQueryLength: MAX_SEARCH_QUERY_LENGTH
    },
    metrics: getDeliveryMetrics()
  };
}
const overview_get = defineAdminEventHandler("admin:delivery:overview", async (event) => {
  await requireAdminRole(event, ["super-admin", "admin"]);
  const search = await collectSearch();
  const graphql = collectGraphQL();
  const guardrails = collectGuardrails();
  const data = {
    graphql,
    search,
    guardrails,
    generatedAt: (/* @__PURE__ */ new Date()).toISOString()
  };
  return {
    success: true,
    data
  };
});

export { overview_get as default };
//# sourceMappingURL=overview.get.mjs.map
