import { NextResponse } from 'next/server';
import { withErrorHandler } from '@server/middleware/withErrorHandler';
import { withAuth, AuthedRequest } from '@server/middleware/withAuth';
import { listDeliveryAttempts } from '@server/services/webhooks.service';

export const GET = withErrorHandler(
  withAuth(async (req: AuthedRequest) => {
    const { searchParams } = new URL(req.url);
    const webhookId = searchParams.get('webhook_id') ?? undefined;
    const attempts = await listDeliveryAttempts(req.session.restaurantId!, webhookId);
    return NextResponse.json({ attempts });
  })
);
