import { f as defineAdminEventHandler, h as requireAdminRole, q as readBody, c as createError, z as withAIGuardrails, B as approxBodyBytes, a as getSchemaManager, K as generateQueryAdvice } 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 queryAdvisor_post = defineAdminEventHandler("admin:ai:query-advisor", async (event) => {
  var _a;
  await requireAdminRole(event, ["super-admin", "admin"]);
  const body = (_a = await readBody(event)) != null ? _a : {};
  if (typeof body.contentType !== "string" || body.contentType.length === 0) {
    throw createError({
      statusCode: 400,
      statusMessage: "Bad Request",
      message: 'Body must include non-empty "contentType".'
    });
  }
  const request = {
    contentType: body.contentType,
    intent: typeof body.intent === "string" ? body.intent : void 0,
    query: parseQuery(body.query)
  };
  const data = await withAIGuardrails({
    advisor: "query",
    event,
    bodyBytes: approxBodyBytes(body),
    inputDigest: {
      contentType: request.contentType,
      hasIntent: Boolean(request.intent),
      hasQuery: Boolean(request.query)
    },
    run: () => {
      const schemas = getSchemaManager().getAllSchemas();
      return generateQueryAdvice(request, { schemas });
    },
    digestOutput: (result) => ({
      suggestions: result.hints.length,
      bySeverity: result.hints.reduce((acc, h) => {
        var _a2;
        acc[h.severity] = ((_a2 = acc[h.severity]) != null ? _a2 : 0) + 1;
        return acc;
      }, {})
    })
  });
  return { success: true, data };
});
function parseQuery(raw) {
  if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
    return void 0;
  }
  const v = raw;
  const result = {};
  const populate = parseStringList(v.populate);
  if (populate) {
    result.populate = populate;
  }
  if (v.filters && typeof v.filters === "object" && !Array.isArray(v.filters)) {
    result.filters = v.filters;
  }
  if (typeof v.sort === "string") {
    result.sort = v.sort;
  }
  if (v.sortOrder === "asc" || v.sortOrder === "desc") {
    result.sortOrder = v.sortOrder;
  }
  const limit = parseFiniteNumber(v.limit);
  if (limit !== void 0) {
    result.limit = limit;
  }
  const offset = parseFiniteNumber(v.offset);
  if (offset !== void 0) {
    result.offset = offset;
  }
  if (typeof v.search === "string") {
    result.search = v.search;
  }
  const searchFields = parseStringList(v.searchFields);
  if (searchFields) {
    result.searchFields = searchFields;
  }
  if (v.aggregate && typeof v.aggregate === "object" && !Array.isArray(v.aggregate)) {
    const aggEntries = Object.entries(v.aggregate).filter(
      (entry) => typeof entry[1] === "string"
    );
    if (aggEntries.length > 0) {
      result.aggregate = Object.fromEntries(aggEntries);
    }
  }
  const groupBy = parseStringList(v.groupBy);
  if (groupBy) {
    result.groupBy = groupBy;
  }
  if (v.having && typeof v.having === "object" && !Array.isArray(v.having)) {
    result.having = v.having;
  }
  if (typeof v.locale === "string") {
    result.locale = v.locale;
  }
  return result;
}
function parseStringList(raw) {
  if (typeof raw === "string") {
    return raw.split(",").map((value) => value.trim()).filter((value) => value.length > 0);
  }
  if (Array.isArray(raw)) {
    return raw.filter((value) => typeof value === "string").map((value) => value.trim()).filter((value) => value.length > 0);
  }
  return void 0;
}
function parseFiniteNumber(raw) {
  if (typeof raw === "number") {
    return Number.isFinite(raw) ? raw : void 0;
  }
  if (typeof raw === "string" && raw.trim().length > 0) {
    const parsed = Number(raw);
    return Number.isFinite(parsed) ? parsed : void 0;
  }
  return void 0;
}

export { queryAdvisor_post as default };
//# sourceMappingURL=query-advisor.post.mjs.map
