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

const API_URL = process.env.NEXT_PUBLIC_API_URL;

/** Thin proxy to Laravel GET /checkout/{order}/status — used for client-side polling (e.g. the Signature wizard's payment step). */
export async function GET(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 apiRes = await fetch(`${API_URL}/checkout/${order}/status`, {
    headers: {
      Accept: "application/json",
      Authorization: `Bearer ${token}`,
    },
    cache: "no-store",
  });

  const data = await apiRes.json();

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