package camel_oil

import (
	"context"

	"github.com/gogf/gf/v2/errors/gcode"
	v1 "kami/api/camel_oil/v1"
	"kami/api/commonApi"
	"kami/internal/errHandler"
	"kami/internal/model"
	"kami/internal/service"
)

func (c *ControllerV1) ListCardBindingsByToken(ctx context.Context, req *v1.ListCardBindingsByTokenReq) (res *v1.ListCardBindingsByTokenRes, err error) {
	_, err = service.SysAuth().LoginWithIFrameAndLogin(ctx)
	if err != nil {
		err = errHandler.WrapError(ctx, gcode.CodeInternalError, err, "登录校验失败")
		return
	}
	bindings, total, err := service.CamelOil().GetCardBindingsByToken(ctx, &model.CamelOilCardListInput{
		TokenId:  req.TokenId,
		Current:  req.Current,
		PageSize: req.PageSize,
	})
	if err != nil {
		return nil, err
	}
	bindingInfos := make([]v1.CardBindingInfo, 0, len(bindings))
	for _, binding := range bindings {
		bindingInfos = append(bindingInfos, v1.CardBindingInfo{
			Id:           binding.Id,
			TokenId:      binding.TokenId,
			Name:         "", // 需要通过查询获取，这里暂时留空
			OrderId:      binding.OrderId,
			CardNumber:   binding.CardNumber,
			CardPassword: binding.CardPassword,
			Amount:       binding.Amount.InexactFloat64(),
			CreatedAt:    binding.CreatedAt,
		})
	}
	return &v1.ListCardBindingsByTokenRes{
		CommonPageRes: commonApi.CommonPageRes[v1.CardBindingInfo]{
			Total: total,
			CommonDataRes: commonApi.CommonDataRes[v1.CardBindingInfo]{
				List: bindingInfos,
			},
		},
	}, nil
}
