import { f as defineAdminEventHandler, h as requireAdminRole, q as readBody, b as getDatabaseManager, z as withAIGuardrails, B as approxBodyBytes, c as createError, a as getSchemaManager, J as generateMigrationAdvice } 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 migrationAdvisor_post = defineAdminEventHandler("admin:ai:migration-advisor", async (event) => {
  var _a;
  await requireAdminRole(event, ["super-admin", "admin"]);
  const body = (_a = await readBody(event)) != null ? _a : {};
  const intent = typeof body.intent === "string" ? body.intent : void 0;
  const dbManager = getDatabaseManager();
  const providedDiff = isSchemaDiffResult(body.diff) ? body.diff : void 0;
  const providedPreview = isMigrationPreview(body.preview) ? body.preview : void 0;
  const data = await withAIGuardrails({
    advisor: "migration",
    event,
    bodyBytes: approxBodyBytes(body),
    inputDigest: {
      intent: intent != null ? intent : null,
      hasProvidedDiff: Boolean(providedDiff),
      hasProvidedPreview: Boolean(providedPreview)
    },
    run: async () => {
      let diff;
      let preview;
      try {
        diff = providedDiff != null ? providedDiff : await dbManager.diffSchemas();
        preview = providedPreview != null ? providedPreview : await dbManager.previewSync(diff);
      } catch (error) {
        if (error instanceof Error && error.message === "Database not connected") {
          throw createError({
            statusCode: 503,
            statusMessage: "Database Not Connected",
            message: "Database is not connected. Please check configuration."
          });
        }
        throw createError({
          statusCode: 500,
          statusMessage: "Failed to compute schema diff",
          message: error instanceof Error ? error.message : "Unknown error"
        });
      }
      const schemas = getSchemaManager().getAllSchemas();
      const request = { intent};
      return generateMigrationAdvice(request, diff, preview, { schemas });
    },
    digestOutput: (result) => ({
      suggestions: result.breakingChanges.length + result.rolloutWarnings.length + result.rollbackHints.length + result.recommendations.length,
      bySeverity: { breaking: result.breakingChanges.length, info: result.recommendations.length }
    })
  });
  return { success: true, data };
});
function isSchemaDiffResult(raw) {
  if (!raw || typeof raw !== "object") {
    return false;
  }
  const v = raw;
  return typeof v.dialect === "string" && typeof v.generatedAt === "string" && Array.isArray(v.schemas) && Array.isArray(v.joinTables) && typeof v.totalChanges === "number" && typeof v.maxRisk === "string" && typeof v.riskSummary === "object" && v.riskSummary !== null;
}
function isMigrationPreview(raw) {
  if (!raw || typeof raw !== "object") {
    return false;
  }
  const v = raw;
  return typeof v.dialect === "string" && typeof v.generatedAt === "string" && Array.isArray(v.statements) && typeof v.totalStatements === "number" && typeof v.maxRisk === "string";
}

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