import { f as defineAdminEventHandler, h as requireAdminRole, q as readBody, c as createError, p as getAIActionByPublicId, t as getAdminActor, y as recordAIAction } 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 ADVISORS = [
  "schema",
  "migration",
  "editorial-qa",
  "scaffolding",
  "query",
  "automation"
];
const CATEGORIES = [
  "schema-suggestion",
  "migration-warning",
  "editorial-issue",
  "scaffolding-template",
  "query-hint",
  "automation-cleanup",
  "automation-bulk",
  "automation-workflow",
  "automation-i18n",
  "automation-media",
  "automation-housekeeping"
];
const TRUSTS = ["high", "medium", "low"];
const SEVERITIES = ["info", "warning", "critical"];
const MAX_TITLE_LENGTH = 500;
const MAX_PUBLIC_ID_LENGTH = 64;
const PUBLIC_ID_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,63}$/;
function isOneOf(value, allowed) {
  return typeof value === "string" && allowed.includes(value);
}
function parseBoundedText(raw, maxLength) {
  if (typeof raw !== "string") {
    return null;
  }
  const value = raw.trim();
  if (value.length === 0 || value.length > maxLength) {
    return null;
  }
  return value;
}
function parsePublicId(raw) {
  if (raw === void 0 || raw === null || raw === "") {
    return void 0;
  }
  const value = parseBoundedText(raw, MAX_PUBLIC_ID_LENGTH);
  if (!value || !PUBLIC_ID_PATTERN.test(value)) {
    return null;
  }
  return value;
}
function parseReviewUrl(raw) {
  if (typeof raw !== "string") {
    return void 0;
  }
  const value = raw.trim();
  if (value.length === 0 || value.length > 1e3 || value.startsWith("//") || /^[a-z][a-z0-9+.-]*:/i.test(value) || !value.startsWith("/")) {
    return void 0;
  }
  return value;
}
function parseSnapshot(raw) {
  if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
    return null;
  }
  const v = raw;
  const title = parseBoundedText(v.title, MAX_TITLE_LENGTH);
  if (!title) {
    return null;
  }
  const out = { title };
  if (typeof v.rationale === "string") {
    out.rationale = v.rationale;
  }
  if (typeof v.recommendation === "string") {
    out.recommendation = v.recommendation;
  }
  if (typeof v.impactCount === "number" && Number.isFinite(v.impactCount)) {
    out.impactCount = v.impactCount;
  }
  if (typeof v.scope === "string") {
    out.scope = v.scope;
  }
  if (Array.isArray(v.sampleIds)) {
    out.sampleIds = v.sampleIds.filter((id) => typeof id === "string" || typeof id === "number").slice(0, 10);
  }
  const reviewUrl = parseReviewUrl(v.reviewUrl);
  if (reviewUrl) {
    out.reviewUrl = reviewUrl;
  }
  if (typeof v.reversible === "boolean") {
    out.reversible = v.reversible;
  }
  if (v.extra && typeof v.extra === "object" && !Array.isArray(v.extra)) {
    out.extra = v.extra;
  }
  return out;
}
const index_post = defineAdminEventHandler("admin:ai:actions:propose", async (event) => {
  var _a;
  await requireAdminRole(event, ["super-admin", "admin"]);
  const body = (_a = await readBody(event)) != null ? _a : {};
  if (!isOneOf(body.advisor, ADVISORS)) {
    throw createError({
      statusCode: 400,
      statusMessage: "Bad Request",
      message: "Invalid advisor."
    });
  }
  if (!isOneOf(body.category, CATEGORIES)) {
    throw createError({
      statusCode: 400,
      statusMessage: "Bad Request",
      message: "Invalid category."
    });
  }
  if (!isOneOf(body.trust, TRUSTS)) {
    throw createError({ statusCode: 400, statusMessage: "Bad Request", message: "Invalid trust." });
  }
  if (!isOneOf(body.severity, SEVERITIES)) {
    throw createError({
      statusCode: 400,
      statusMessage: "Bad Request",
      message: "Invalid severity."
    });
  }
  if (typeof body.title !== "string" || body.title.length === 0) {
    throw createError({
      statusCode: 400,
      statusMessage: "Bad Request",
      message: "title is required."
    });
  }
  const title = parseBoundedText(body.title, MAX_TITLE_LENGTH);
  if (!title) {
    throw createError({
      statusCode: 400,
      statusMessage: "Bad Request",
      message: `title must be 1-${MAX_TITLE_LENGTH} characters.`
    });
  }
  const snapshot = parseSnapshot(body.snapshot);
  if (!snapshot) {
    throw createError({
      statusCode: 400,
      statusMessage: "Bad Request",
      message: "snapshot must include title."
    });
  }
  const publicId = parsePublicId(body.publicId);
  if (publicId === null) {
    throw createError({
      statusCode: 400,
      statusMessage: "Bad Request",
      message: "publicId must be 1-64 chars and contain only letters, numbers, dot, underscore, colon or dash."
    });
  }
  if (publicId) {
    const existing = await getAIActionByPublicId(publicId);
    if (existing) {
      return { success: true, data: existing };
    }
  }
  const actor = getAdminActor(event);
  const input = {
    advisor: body.advisor,
    category: body.category,
    trust: body.trust,
    severity: body.severity,
    title,
    snapshot,
    proposedBy: { type: "admin", id: actor.id, name: actor.displayName },
    ttlSeconds: typeof body.ttlSeconds === "number" && Number.isFinite(body.ttlSeconds) && body.ttlSeconds > 0 ? Math.floor(body.ttlSeconds) : void 0,
    publicId
  };
  const entry = await recordAIAction(input);
  return { success: true, data: entry };
});

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