import { NextResponse } from "next/server"
import { handleEsignWebhook, getAdapter } from "@/lib/esign"

// Inbound e-signature status webhook (DocuSign Connect / Dropbox Sign callback).
export async function POST(request: Request, { params }: { params: Promise<{ provider: string }> }) {
  const { provider } = await params
  if (!getAdapter(provider)) return NextResponse.json({ error: "unknown provider" }, { status: 404 })

  const body = await request.text()
  try {
    await handleEsignWebhook(provider, body, request.headers)
  } catch {
    // never fail the webhook — providers retry on non-2xx
  }

  // Dropbox Sign requires this exact response body to validate the callback URL.
  if (provider === "dropbox_sign") return new Response("Hello API Event Received", { status: 200 })
  return NextResponse.json({ ok: true })
}
