import { NextResponse } from "next/server"
import { prisma } from "@/lib/prisma"

// Donor self-reports "I've donated" on external platform
export async function POST(_req: Request, { params }: { params: Promise<{ id: string }> }) {
  try {
    const { id } = await params
    
    const pledge = await prisma.pledge.findUnique({ where: { id } })
    if (!pledge) {
      return NextResponse.json({ error: "Not found" }, { status: 404 })
    }

    // Only update if currently "new" or "initiated"
    if (pledge.status === "new" || pledge.status === "initiated") {
      await prisma.pledge.update({
        where: { id },
        data: { status: "initiated" },
      })
    }

    return NextResponse.json({ ok: true, status: "initiated" })
  } catch (e) {
    console.error("mark-initiated error:", e)
    return NextResponse.json({ error: "Failed" }, { status: 500 })
  }
}
