package bootstrap

import (
	"go.uber.org/zap"

	"github.com/break/junhong_cmp_fiber/internal/infrastructure/systemconfig"
	"github.com/break/junhong_cmp_fiber/pkg/constants"
)

func registerCarrierCallbackConfigDefinitions(registry *systemconfig.Registry, logger *zap.Logger) {
	if registry == nil {
		return
	}
	definitions := []systemconfig.Definition{
		{Key: constants.SystemConfigCarrierCallbackCTCCRealnameEnabled, Module: constants.SystemConfigModuleCarrierCallback, ValueType: constants.SystemConfigTypeBool, DefaultValue: "false", Description: "是否处理中国电信实名回调", Control: "switch"},
		{Key: constants.SystemConfigCarrierCallbackCMCCRealnameEnabled, Module: constants.SystemConfigModuleCarrierCallback, ValueType: constants.SystemConfigTypeBool, DefaultValue: "false", Description: "是否处理中国移动实名回调", Control: "switch"},
		{Key: constants.SystemConfigCarrierCallbackCUCCRealnameEnabled, Module: constants.SystemConfigModuleCarrierCallback, ValueType: constants.SystemConfigTypeBool, DefaultValue: "false", Description: "是否处理中国联通实名成功回调", Control: "switch"},
		{Key: constants.SystemConfigCarrierCallbackCUCCRealnameRemovalEnabled, Module: constants.SystemConfigModuleCarrierCallback, ValueType: constants.SystemConfigTypeBool, DefaultValue: "false", Description: "是否处理中国联通解除实名回调", Control: "switch"},
	}
	for _, definition := range definitions {
		if existing, exists := registry.Get(definition.Key); exists {
			if existing.ValueType != definition.ValueType || existing.Module != definition.Module {
				logCarrierCallbackConfigRegistrationError(logger, definition.Key, "配置 Key 已被其他类型或模块注册")
			}
			continue
		}
		if err := registry.Register(definition); err != nil {
			logCarrierCallbackConfigRegistrationError(logger, definition.Key, err.Error())
		}
	}
}

func logCarrierCallbackConfigRegistrationError(logger *zap.Logger, key, reason string) {
	if logger != nil {
		logger.Error("注册运营商回调系统配置失败，相关回调将按关闭处理", zap.String("config_key", key), zap.String("reason", reason))
	}
}
