import { NextRequest, NextResponse } from 'next/server';

import { withAuth, withPermission } from '@/lib/api/helpers';
import { errorResponse } from '@/lib/errors';
import { regenerateSecret } from '@/lib/services/webhooks.service';

// ─── POST /api/v1/admin/webhooks/[webhookId]/regenerate-secret ────────────────

export const POST = withAuth(
  withPermission('admin', 'manage_webhooks', async (_req: NextRequest, ctx, params) => {
    try {
      const { webhookId } = params;
      const data = await regenerateSecret(ctx.portId, webhookId!, {
        userId: ctx.userId,
        portId: ctx.portId,
        ipAddress: ctx.ipAddress,
        userAgent: ctx.userAgent,
      });
      return NextResponse.json({ data });
    } catch (error) {
      return errorResponse(error);
    }
  }),
);
