package card_info_apple

import (
	"context"
	"github.com/duke-git/lancet/v2/slice"
	"github.com/duke-git/lancet/v2/strutil"
	"github.com/gogf/gf/v2/text/gstr"
	"github.com/jinzhu/copier"
	"kami/api/commonApi"
	"kami/internal/errHandler"
	"kami/internal/model"
	"kami/internal/service"

	"kami/api/card_info_apple/v1"

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

func (c *ControllerV1) CardInfoList(ctx context.Context, req *v1.CardInfoListReq) (res *v1.CardInfoListRes, err error) {
	userInfo, err := service.SysAuth().LoginWithIFrameAndLogin(ctx)
	if err != nil {
		err = errHandler.WrapError(ctx, gcode.CodeNotAuthorized, err, "权限不足")
		return
	}
	appleAccountService := service.AppleAccount()
	total, list, err := appleAccountService.List(ctx, &model.CardInfoParamsInput{
		CommonPageReq: req.CommonPageReq,
		Account:       req.Account,
		UploadUserId:  userInfo.Id,
	})
	slice.ForEach(list, func(index int, item model.AppleAccountListOutput) {
		//password := item.Password
		// 把密码都替换为 *
		list[index].Password = strutil.HideString(item.Password, 0, gstr.LenRune(item.Password), "*")
	})

	res = &v1.CardInfoListRes{
		CommonPageRes: commonApi.CommonPageRes[v1.AppleCardListRecord]{
			Total: total,
			CommonDataRes: commonApi.CommonDataRes[v1.AppleCardListRecord]{
				List: make([]v1.AppleCardListRecord, 0),
			},
		},
	}
	_ = copier.CopyWithOption(&res.List, &list, copier.Option{IgnoreEmpty: true, DeepCopy: true})
	return
}
