package card_info_walmart

import (
	"context"
	"fmt"
	"kami/api/commonApi"
	"kami/internal/consts"
	"kami/internal/model"
	"kami/internal/service"

	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"

	"kami/api/card_info_walmart/v1"
)

func (c *ControllerV1) AccountUpdate(ctx context.Context, req *v1.AccountUpdateReq) (res *v1.AccountUpdateRes, err error) {
	maxLimit := service.SysConfigDict().GetMaxAccountRedeemCount(ctx, consts.CardRedeemAccountCategoryWalMart)
	if req.MaxCountLimit > maxLimit {
		err = gerror.NewCode(gcode.CodeInvalidOperation, fmt.Sprintf("上传卡密超过管理员设置限制，管理员设置最大次数：%d", maxLimit))
		return
	}
	err = service.CardRedeemAccount().Update(ctx, &model.RedeemAccountUpdateInput{
		CommonStrId: commonApi.CommonStrId{
			ID: req.ID,
		},
		Status:         req.Status,
		Cookie:         req.Cookie,
		Name:           req.Name,
		Remark:         req.Remark,
		GroupId:        req.GroupId,
		MaxAmountLimit: req.MaxAmountLimit,
		MaxCountLimit:  req.MaxCountLimit,
	}, nil)
	return
}
