import { getServerSession } from "next-auth";
import { authOptions } from "@utils/auth";
import { redirect, RedirectType } from 'next/navigation';
import {serverGraphqlFetch} from "@utils/bagisto/index";
import CheckoutWrapper from "./_components/CheckoutWrapper";
import { GET_CART_ITEM } from "@/graphql";

import { GetCartItemData } from "@/types/cart/type";

/**
 * 进入结账页 
 * 登录用户的话，获取用户地址列表 从auth session 中获取email
 * 获取billing地址和shipping地址
 * 获取购物车数据
 * 获取运输方式
 * 获取支付方式
 *  
 * 
 */
export const dynamic = "force-dynamic";

export default async function CheckoutPage() {

    const session = await getServerSession(authOptions); // 游客是null


    const {data: cartDetailsRes} = await serverGraphqlFetch<GetCartItemData>({
        query: GET_CART_ITEM,
        operationName:'GetCartItem'
    });
    if(cartDetailsRes.createReadCart === null) {
        redirect('/', RedirectType.replace);
    }
    const cartDetails = cartDetailsRes.createReadCart.readCart;
    return (
        <>
            <CheckoutWrapper key={cartDetails.id}
                cartDetailData={cartDetails}
                loginEmail={session?.user?.email || ""}
            />
        </>
    );
  
}
