import { NextRequest, NextResponse } from "next/server"
import { sendPledgeReceipt, sendPledgeReminder, getWhatsAppStatus } from "@/lib/whatsapp"

export async function POST(request: NextRequest) {
  try {
    const body = await request.json()
    const { type, phone, data } = body

    if (!phone) {
      return NextResponse.json({ error: "Phone number required" }, { status: 400 })
    }

    let result
    switch (type) {
      case "receipt":
        result = await sendPledgeReceipt(phone, data)
        break
      case "reminder":
        result = await sendPledgeReminder(phone, data)
        break
      default:
        return NextResponse.json({ error: "Unknown message type" }, { status: 400 })
    }

    return NextResponse.json(result)
  } catch (error) {
    console.error("WhatsApp send error:", error)
    return NextResponse.json({ error: "Failed to send" }, { status: 500 })
  }
}

export async function GET() {
  const status = await getWhatsAppStatus()
  return NextResponse.json(status)
}
