import { d as defineEventHandler, r as requireAuth, c as createError, cI as requireAdminScope, i as requireApiTokenScopes, eY as getWebhookById, f3 as getWebhookLogById, f4 as replayWebhookDelivery } 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 _logId__post = defineEventHandler(async (event) => {
  var _a, _b, _c, _d;
  try {
    await requireAuth(event);
    if (!event.context.adminUser) {
      throw createError({
        statusCode: 403,
        statusMessage: "Forbidden",
        message: "Only administrators can manage webhooks"
      });
    }
    await requireAdminScope(event, "webhooks:write");
    requireApiTokenScopes(event.context.apiToken, { scopes: ["webhooks:write"] });
    const id = (_a = event.context.params) == null ? void 0 : _a.id;
    const logId = (_b = event.context.params) == null ? void 0 : _b.logId;
    if (!id || !logId) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "Webhook ID and log ID are required"
      });
    }
    const numericId = parseInt(id);
    const numericLogId = parseInt(logId);
    if (isNaN(numericId) || isNaN(numericLogId)) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "IDs must be valid numbers"
      });
    }
    const webhook = await getWebhookById(numericId);
    if (!webhook) {
      throw createError({
        statusCode: 404,
        statusMessage: "Not Found",
        message: "Webhook not found"
      });
    }
    const log = await getWebhookLogById(numericLogId);
    if (!log || log.webhook_id !== numericId) {
      throw createError({
        statusCode: 404,
        statusMessage: "Not Found",
        message: "Log entry not found for this webhook"
      });
    }
    const result = await replayWebhookDelivery(numericLogId);
    auditFromEvent(event, "webhook.replay", {
      target: { entityType: "webhook", entityId: webhook.id, displayName: webhook.name },
      metadata: { logId: numericLogId, deliveryId: result.deliveryId }
    });
    return {
      success: true,
      data: {
        message: "Delivery replay queued",
        deliveryId: result.deliveryId,
        originalLogId: numericLogId
      }
    };
  } catch (error) {
    if (error && typeof error === "object" && "statusCode" in error) {
      throw error;
    }
    console.error(
      `[POST /api/webhooks/${(_c = event.context.params) == null ? void 0 : _c.id}/replay/${(_d = event.context.params) == null ? void 0 : _d.logId}] Error:`,
      error
    );
    throw createError({
      statusCode: 500,
      statusMessage: "Failed to replay webhook delivery",
      message: error instanceof Error ? error.message : "Unknown error"
    });
  }
});

export { _logId__post as default };
//# sourceMappingURL=_logId_.post.mjs.map
