// Code generated by hertz generator.

package checkout

import (
	"apis/biz/utils"
	"apis/hertz_gen/api/checkout"
	"apis/rpc"
	"context"
	"github.com/BeroKiTeer/MyGoMall/common/kitex_gen/auth"
	checkout_kitex "github.com/BeroKiTeer/MyGoMall/common/kitex_gen/checkout"
	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/protocol/consts"
	"github.com/cloudwego/kitex/pkg/klog"
)

// Checkout .
// @router /api/checkout [POST]
func Checkout(ctx context.Context, c *app.RequestContext) {
	var err error
	var req checkout.CheckoutReq
	var urlCallback string //重定向的url
	err = c.BindAndValidate(&req)
	if err != nil {
		c.String(consts.StatusBadRequest, err.Error())
		return
	}
	err = utils.BindJson(c, &req)
	klog.Info(req.UserId)
	//获取请求头的token
	token := c.Request.Header.Get("Authorization")
	if token == "" {
		utils.SendErrResponse(ctx, c, consts.StatusUnauthorized, err)
		return
	}

	if err != nil {

		utils.SendErrResponse(ctx, c, consts.StatusBadRequest, err)
		return
	}
	//获取用户id
	rawID, err := rpc.AuthClient.DecodeToken(ctx, &auth.DecodeTokenReq{Token: token})
	if err != nil {
		utils.SendErrResponse(ctx, c, consts.StatusInternalServerError, err)
		return
	}
	reqRpc := checkout_kitex.CheckoutReq{
		UserId:        uint32(rawID.UserId),
		Firstname:     req.Firstname,
		Lastname:      req.Lastname,
		Email:         req.Email,
		Address:       req.Address,
		Items:         nil,
		PaymentMethod: req.PaymentMethod,
	}
	//调用后端rpc服务的方法返回金额与订单号
	checkoutResp, err := rpc.CheckoutClient.Checkout(ctx, &reqRpc)
	if err != nil {
		klog.Error(err)
		utils.SendErrResponse(ctx, c, consts.StatusServiceUnavailable, err)
		return
	}

	utils.SendSuccessResponse(ctx, c, consts.StatusOK, checkout.CheckoutResp{
		OrderId:     checkoutResp.OrderId,
		UrlCallback: urlCallback,
		Amount:      checkoutResp.Amount,
	})

}
