package agent

import (
	"context"

	"xingfucha-server/app/main/api/internal/svc"
	"xingfucha-server/app/main/api/internal/types"
	"xingfucha-server/app/main/model"
	"xingfucha-server/common/xerr"
	"xingfucha-server/pkg/lzkit/lzUtils"

	"github.com/jinzhu/copier"
	"github.com/pkg/errors"
	"github.com/zeromicro/go-zero/core/logx"
)

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

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

func (l *GetMembershipInfoLogic) GetMembershipInfo() (resp *types.GetMembershipInfoResp, err error) {
	// 获取普通代理配置
	normalConfig, err := l.svcCtx.AgentMembershipConfigModel.FindOneByLevelName(l.ctx, model.AgentLeveNameNormal)
	if err != nil {
		return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "获取普通代理配置失败: %v", err)
	}

	// 获取VIP会员配置
	vipConfig, err := l.svcCtx.AgentMembershipConfigModel.FindOneByLevelName(l.ctx, model.AgentLeveNameVIP)
	if err != nil {
		return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "获取VIP会员配置失败: %v", err)
	}

	// 获取SVIP会员配置
	svipConfig, err := l.svcCtx.AgentMembershipConfigModel.FindOneByLevelName(l.ctx, model.AgentLeveNameSVIP)
	if err != nil {
		return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "获取SVIP会员配置失败: %v", err)
	}

	// 转换配置的辅助函数
	convertConfig := func(config *model.AgentMembershipConfig) (types.MembershipConfigInfo, error) {
		var configInfo types.MembershipConfigInfo
		err := copier.Copy(&configInfo, config)
		if err != nil {
			return configInfo, err
		}
		// 转换Null类型字段
		configInfo.Price = lzUtils.NullFloat64ToFloat64(config.Price)
		configInfo.ReportCommission = lzUtils.NullFloat64ToFloat64(config.ReportCommission)
		configInfo.LowerActivityReward = lzUtils.NullFloat64ToFloat64(config.LowerActivityReward)
		configInfo.NewActivityReward = lzUtils.NullFloat64ToFloat64(config.NewActivityReward)
		configInfo.LowerStandardCount = lzUtils.NullInt64ToInt64(config.LowerStandardCount)
		configInfo.NewLowerStandardCount = lzUtils.NullInt64ToInt64(config.NewLowerStandardCount)
		configInfo.LowerWithdrawRewardRatio = lzUtils.NullFloat64ToFloat64(config.LowerWithdrawRewardRatio)
		configInfo.LowerConvertVipReward = lzUtils.NullFloat64ToFloat64(config.LowerConvertVipReward)
		configInfo.LowerConvertSvipReward = lzUtils.NullFloat64ToFloat64(config.LowerConvertSvipReward)
		configInfo.ExemptionAmount = lzUtils.NullFloat64ToFloat64(config.ExemptionAmount)
		configInfo.PriceIncreaseMax = lzUtils.NullFloat64ToFloat64(config.PriceIncreaseMax)
		configInfo.PriceRatio = lzUtils.NullFloat64ToFloat64(config.PriceRatio)
		configInfo.PriceIncreaseAmount = lzUtils.NullFloat64ToFloat64(config.PriceIncreaseAmount)
		return configInfo, nil
	}

	// 转换普通代理配置
	normalConfigInfo, err := convertConfig(normalConfig)
	if err != nil {
		return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "转换普通代理配置失败: %v", err)
	}

	// 转换VIP配置
	vipConfigInfo, err := convertConfig(vipConfig)
	if err != nil {
		return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "转换VIP配置失败: %v", err)
	}

	// 转换SVIP配置
	svipConfigInfo, err := convertConfig(svipConfig)
	if err != nil {
		return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "转换SVIP配置失败: %v", err)
	}

	// 构建响应数据
	resp = &types.GetMembershipInfoResp{
		NormalConfig: normalConfigInfo,
		VipConfig:    vipConfigInfo,
		SvipConfig:   svipConfigInfo,
	}

	return resp, nil
}
