package agent

import (
	"context"
	"znc-server/app/main/model"
	"znc-server/common/ctxdata"
	"znc-server/common/xerr"
	"znc-server/pkg/lzkit/lzUtils"

	"github.com/jinzhu/copier"
	"github.com/pkg/errors"

	"znc-server/app/main/api/internal/svc"
	"znc-server/app/main/api/internal/types"

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

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

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

func (l *GetAgentMembershipProductConfigLogic) GetAgentMembershipProductConfig(req *types.AgentMembershipProductConfigReq) (resp *types.AgentMembershipProductConfigResp, err error) {
	userID, err := ctxdata.GetUidFromCtx(l.ctx)
	if err != nil {
		return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取会员用户报告配置，获取用户ID失败: %v", err)
	}

	agentModel, err := l.svcCtx.AgentModel.FindOneByUserId(l.ctx, userID)
	if err != nil {
		return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取会员用户报告配置，获取代理信息失败: %v", err)
	}
	if agentModel.LevelName == "" {
		agentModel.LevelName = model.AgentLeveNameNormal
	}
	agentMembershipConfigModel, err := l.svcCtx.AgentMembershipConfigModel.FindOneByLevelName(l.ctx, agentModel.LevelName)
	if err != nil {
		return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取会员用户报告配置，获取平台配置会员信息失败: %v", err)
	}
	agentMembershipUserConfigModel, err := l.svcCtx.AgentMembershipUserConfigModel.FindOneByAgentIdProductId(l.ctx, agentModel.Id, req.ProductID)
	if err != nil && !errors.Is(err, model.ErrNotFound) {
		return nil, err
	}
	var agentMembershipUserConfig types.AgentMembershipUserConfig
	if agentMembershipUserConfigModel != nil {
		err = copier.Copy(&agentMembershipUserConfig, agentMembershipUserConfigModel)
		if err != nil {
			return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取会员用户报告配置，复制平台配置会员信息失败: %v", err)
		}
	} else {
		agentMembershipUserConfig.ProductID = req.ProductID
	}
	agentProductConfigModelAll, err := l.svcCtx.AgentProductConfigModel.FindOneByProductId(l.ctx, req.ProductID)
	if err != nil {
		return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "获取会员用户报告配置, 获取产品配置%v", err)
	}

	var productConfig types.ProductConfig
	err = copier.Copy(&productConfig, agentProductConfigModelAll)
	if err != nil {
		return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取会员用户报告配置，复制平台产品配置失败: %v", err)
	}
	return &types.AgentMembershipProductConfigResp{
		AgentMembershipUserConfig: agentMembershipUserConfig,
		ProductConfig:             productConfig,
		PriceIncreaseAmount:       lzUtils.NullFloat64ToFloat64(agentMembershipConfigModel.PriceIncreaseAmount),
		PriceIncreaseMax:          lzUtils.NullFloat64ToFloat64(agentMembershipConfigModel.PriceIncreaseMax),
		PriceRatio:                lzUtils.NullFloat64ToFloat64(agentMembershipConfigModel.PriceRatio),
	}, nil
}
