package card_info_walmart

import (
	"context"
	"kami/api/card_info_walmart/v1"
	"kami/internal/consts"
	"kami/internal/service"
	"kami/utility/integration/redeem"
	redeemClient "kami/utility/integration/redeem/client"

	"github.com/duke-git/lancet/v2/pointer"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/os/glog"
)

func (c *ControllerV1) AccountStatusDetect(ctx context.Context, req *v1.AccountStatusDetectReq) (res *v1.AccountStatusDetectRes, err error) {
	res = &v1.AccountStatusDetectRes{
		Status: false,
	}
	accountInfo, err := service.CardRedeemAccount().GetOneById(ctx, req.ID, nil)
	if err != nil || pointer.IsNil(accountInfo) {
		err = gerror.Wrap(err, "获取账户信息失败")
		return
	}
	client, err2 := redeemClient.GetClient(consts.CardRedeemAccountCategoryWalMart)
	if err2 != nil {
		res.Status = false
		glog.Error(ctx, "获取客户端失败", err2)
		return
	}
	res2, err2 := client.QueryBalance(ctx, &redeem.QueryBalanceReq{
		Cookie:    accountInfo.Cookie,
		AccountId: accountInfo.Id,
	})
	if !pointer.IsNil(res2) && res2.Ok {
		res.Status = true
	}
	return
}
