package admin_agent

import (
	"context"
	"strconv"

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

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

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

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

func (l *AdminGetAgentConfigLogic) AdminGetAgentConfig() (resp *types.AdminGetAgentConfigResp, err error) {
	// 获取配置值的辅助函数
	getConfigFloat := func(key string) float64 {
		config, err := l.svcCtx.AgentConfigModel.FindOneByConfigKey(l.ctx, key)
		if err != nil {
			return 0
		}
		value, _ := strconv.ParseFloat(config.ConfigValue, 64)
		return value
	}
	getConfigInt := func(key string) int64 {
		config, err := l.svcCtx.AgentConfigModel.FindOneByConfigKey(l.ctx, key)
		if err != nil {
			return 0
		}
		value, _ := strconv.ParseInt(config.ConfigValue, 10, 64)
		return value
	}

	// 获取等级加成配置
	level1Bonus := getConfigFloat("level_1_bonus")
	level2Bonus := getConfigFloat("level_2_bonus")
	level3Bonus := getConfigFloat("level_3_bonus")

	// 获取升级费用配置
	upgradeToGoldFee := getConfigFloat("upgrade_to_gold_fee")
	upgradeToDiamondFee := getConfigFloat("upgrade_to_diamond_fee")

	// 获取升级返佣配置
	upgradeToGoldRebate := getConfigFloat("upgrade_to_gold_rebate")
	upgradeToDiamondRebate := getConfigFloat("upgrade_to_diamond_rebate")

	// 获取直接上级返佣配置
	directParentAmountDiamond := getConfigFloat("direct_parent_amount_diamond")
	directParentAmountGold := getConfigFloat("direct_parent_amount_gold")
	directParentAmountNormal := getConfigFloat("direct_parent_amount_normal")

	// 获取黄金代理最大返佣金额
	maxGoldRebateAmount := getConfigFloat("max_gold_rebate_amount")

	// 获取佣金冻结配置
	commissionFreezeRatio := getConfigFloat("commission_freeze_ratio")
	commissionFreezeThreshold := getConfigFloat("commission_freeze_threshold")
	commissionFreezeDays := getConfigInt("commission_freeze_days")

	goldUplift := getConfigFloat("gold_max_uplift_amount")
	diamondUplift := getConfigFloat("diamond_max_uplift_amount")
	return &types.AdminGetAgentConfigResp{
		LevelBonus: types.LevelBonusConfig{
			Normal:  int64(level1Bonus),
			Gold:    int64(level2Bonus),
			Diamond: int64(level3Bonus),
		},
		UpgradeFee: types.UpgradeFeeConfig{
			NormalToGold:    upgradeToGoldFee,
			NormalToDiamond: upgradeToDiamondFee,
		},
		UpgradeRebate: types.UpgradeRebateConfig{
			NormalToGoldRebate: upgradeToGoldRebate,
			ToDiamondRebate:    upgradeToDiamondRebate,
		},
		DirectParentRebate: types.DirectParentRebateConfig{
			Diamond: directParentAmountDiamond,
			Gold:    directParentAmountGold,
			Normal:  directParentAmountNormal,
		},
		MaxGoldRebateAmount: maxGoldRebateAmount,
		CommissionFreeze: types.CommissionFreezeConfig{
			Ratio:     commissionFreezeRatio,
			Threshold: commissionFreezeThreshold,
			Days:      commissionFreezeDays,
		},
		TaxRate:                getConfigFloat("tax_rate"),
		TaxExemptionAmount:     getConfigFloat("tax_exemption_amount"),
		GoldMaxUpliftAmount:    goldUplift,
		DiamondMaxUpliftAmount: diamondUplift,
	}, nil
}
