import { f as defineAdminEventHandler, h as requireAdminRole, j as getQuery, b as getDatabaseManager, a as getSchemaManager, ez as emitSchemaSync, c as createError } from '../../../nitro/nitro.mjs';
import { a as auditFromEvent } from '../../../_/audit-log-helpers.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 '../../../_/audit-log-actor.mjs';

const safeSync_post = defineAdminEventHandler("admin:database:safe-sync", async (event) => {
  try {
    await requireAdminRole(event, ["super-admin", "admin"]);
    const query = getQuery(event);
    const dryRun = query.dryRun === "true" || query.dryRun === "1";
    const force = query.force === "true" || query.force === "1";
    const dbManager = getDatabaseManager();
    const result = await dbManager.safeSyncSchemas({ dryRun, force });
    if (result.success && !dryRun && result.applied.length > 0) {
      const schemaNames = getSchemaManager().getAllSchemas().map((s) => s.info.singularName);
      await emitSchemaSync(schemaNames);
    }
    const message = !result.success && result.blockedChanges.length > 0 && !dryRun && !force ? `Safe sync \u0437\u0430\u0431\u043B\u043E\u043A\u0438\u0440\u043E\u0432\u0430\u043D: \u043E\u0431\u043D\u0430\u0440\u0443\u0436\u0435\u043D\u044B \u0438\u0437\u043C\u0435\u043D\u0435\u043D\u0438\u044F \u0441 \u0440\u0438\u0441\u043A\u043E\u043C ${result.maxRisk}` : dryRun ? `Dry run: ${result.summary.total} \u043E\u043F\u0435\u0440\u0430\u0446\u0438\u0439 (\u043D\u0435 \u043F\u0440\u0438\u043C\u0435\u043D\u0435\u043D\u043E)` : `\u041F\u0440\u0438\u043C\u0435\u043D\u0435\u043D\u043E ${result.summary.applied} \u0438\u0437 ${result.summary.total} \u043E\u043F\u0435\u0440\u0430\u0446\u0438\u0439`;
    auditFromEvent(event, "db.safe_sync", {
      metadata: {
        dryRun,
        force,
        total: result.summary.total,
        applied: result.summary.applied,
        blockedChanges: result.blockedChanges.length,
        maxRisk: result.maxRisk
      },
      outcome: result.success ? "success" : "failed"
    });
    return {
      success: result.success,
      dryRun,
      force,
      message,
      data: result
    };
  } catch (error) {
    if (error && typeof error === "object" && "statusCode" in error) {
      throw error;
    }
    console.error("[POST /api/database/safe-sync] Error:", 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 perform safe sync",
      message: error instanceof Error ? error.message : "Unknown error"
    });
  }
});

export { safeSync_post as default };
//# sourceMappingURL=safe-sync.post.mjs.map
