import { NextResponse } from "next/server"
import { isAuthorizedCron } from "@/lib/cron-auth"
import { processDueDeliveries } from "@/lib/webhooks/deliver"

// Webhook delivery retry drain. The emitter attempts an immediate delivery when
// an event fires; this cron re-attempts anything still pending whose backoff
// window has elapsed (and covers deliveries orphaned by a process restart).
// Scheduled every 5 minutes via DigitalOcean Functions — see functions/project.yml.
export async function GET(request: Request) {
  if (!isAuthorizedCron(request)) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
  }

  const { processed, delivered } = await processDueDeliveries(200)
  return NextResponse.json({ processed, delivered })
}
