import { NextRequest, NextResponse } from "next/server";
import { restApiFetch } from "@/utils/bagisto";
import { isBagistoError } from "@/utils/type-guards";
import { getAuthToken } from "@/utils/helper";
export async function POST(req: NextRequest) {
  try {
    const authorizationToken = getAuthToken(req); // 获取headers中的Authorization的值
    const params = await req.json();
    const response = await restApiFetch<{
      data: any; // 这个是返回结果的数据类型，暂时写成any，具体看后端反的数据结构再改成确定的类型1
      variables:{
        categorys:string,
      } ;
    }>({
      api: "/customer/reward-points/browse-product",
      method: "POST",
      cache: "no-store",
      variables: params,
      guestToken: authorizationToken,
    });
    // 打印后端原始返回结构
    console.log(
      "接口原始response.body =",
      JSON.stringify(response.body, null, 2),
    );
    return NextResponse.json({
      status: response.status,
      data: response.body,
    });
  } catch (error) {
    console.log("/customer/token/address---", error); // 调试用
    if (isBagistoError(error)) {
      return NextResponse.json(
        {
          data: null,
          error: error.cause ?? error,
        },
        { status: 200 },
      );
    }

    return NextResponse.json(
      {
        message: "Network error",
        error: error instanceof Error ? error.message : error,
      },
      { status: 500 },
    );
  }
}