import { NextResponse } from "next/server";
import Stripe from "stripe";

export async function POST(request: any) {
  const body = await request.json();
  const result = await checkoutPayment(body);
  return NextResponse.json(result);
}

const checkoutPayment = async (body: any) => {
  const stripe = new Stripe(process.env.STRIPE_SECRET_KEY as string);

  const { firstName, email } = body;
  let customerid = "";
  const customer = await stripe.customers.create({
    name: firstName,
    email: email,
  });
  customerid = customer?.id;
  const lineItems = [
    {
      price_data: {
        currency: "usd",
        product_data: {
          name: "PitchDeck",
        },
        unit_amount: 29 * 100,
      },
      quantity: 1,
    },
  ];
  const metaData = {
    firstName: firstName,
    email: email,
  };

  const session = await stripe.checkout.sessions.create({
    customer: customerid,
    line_items: lineItems,
    mode: "payment",
    metadata: metaData,
    success_url: `${process.env.BASE_URL}/profile?paid=success`,
    cancel_url: `${process.env.BASE_URL}/`,
  });

  return { session };
};
