package admin_agent

import (
	"context"
	"errors"

	"tydata-server/app/main/api/internal/svc"
	"tydata-server/app/main/api/internal/types"
	"tydata-server/app/main/model"
	"tydata-server/common/xerr"

	"github.com/zeromicro/go-zero/core/logx"
)

type AdminGetAgentWalletLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAdminGetAgentWalletLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminGetAgentWalletLogic {
	return &AdminGetAgentWalletLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AdminGetAgentWalletLogic) AdminGetAgentWallet(req *types.AdminGetAgentWalletReq) (resp *types.AdminGetAgentWalletResp, err error) {
	// 查询代理钱包信息
	wallet, err := l.svcCtx.AgentWalletModel.FindOneByAgentId(l.ctx, req.AgentId)
	if err != nil {
		if errors.Is(err, model.ErrNotFound) {
			return nil, xerr.NewErrMsg("代理钱包不存在")
		}
		return nil, err
	}

	resp = &types.AdminGetAgentWalletResp{
		Balance:       wallet.Balance,
		FrozenBalance: wallet.FrozenBalance,
		TotalEarnings: wallet.TotalEarnings,
	}
	return
}
