import { createCheckoutSession } from "@/lib/areeba";

export async function POST(req: Request) {
  const body = await req.json();
  const { name, email, amount, returnUrl } = body;

  if (!name || !email || !amount)
    return new Response(
      JSON.stringify({ error: "Missing required payment information." }),
      { status: 400 },
    );

  try {
    const orderId = `waseel-${Date.now()}`;

    const session = await createCheckoutSession({
      orderId,
      amount: parseFloat(amount),
      currency: "USD",
      description: `Waseel ride payment for ${name}`,
      returnUrl: returnUrl || "waseel://book-ride",
    });

    return new Response(
      JSON.stringify({
        orderId,
        checkoutUrl: session.checkoutUrl,
        successIndicator: session.successIndicator,
      }),
    );
  } catch (err) {
    console.log("[AREEBA_PAYMENT_CREATE]: ", err);

    return new Response(JSON.stringify({ error: "Internal Server Error" }), {
      status: 500,
    });
  }
}
