import { NextResponse } from "next/server";
import { getToken } from "@/lib/session";

const API_URL = process.env.NEXT_PUBLIC_API_URL;

/** Thin proxy to Laravel POST /checkout/{order}/pay — charges Core API for the chosen payment method. */
export async function POST(request: Request, { params }: { params: Promise<{ order: string }> }) {
  const token = await getToken();

  if (!token) {
    return NextResponse.json({ message: "You must log in first." }, { status: 401 });
  }

  const { order } = await params;
  const body = await request.json();

  const apiRes = await fetch(`${API_URL}/checkout/${order}/pay`, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Accept: "application/json",
      Authorization: `Bearer ${token}`,
    },
    body: JSON.stringify(body),
  });

  const data = await apiRes.json();

  return NextResponse.json(data, { status: apiRes.status });
}
