package admin_agent

import (
	"context"
	"strconv"

	"jnc-server/app/main/api/internal/svc"
	"jnc-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
	}

	// 系统简化：移除等级加成、升级费用、升级返佣、直接上级返佣配置、免税额度
	// 只保留佣金冻结配置和税率配置

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

	return &types.AdminGetAgentConfigResp{
		CommissionFreeze: types.CommissionFreezeConfig{
			Ratio:     commissionFreezeRatio,
			Threshold: commissionFreezeThreshold,
			Days:      commissionFreezeDays,
		},
		TaxRate: getConfigFloat("tax_rate"),
	}, nil
}
