package bootstrap

import (
	"go.uber.org/zap"

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

func registerPaymentMethodConfigDefinitions(registry *systemconfig.Registry, logger *zap.Logger) {
	if registry == nil {
		return
	}
	definitions := []systemconfig.Definition{
		{Key: constants.SystemConfigPaymentAllowedCard, Module: constants.SystemConfigModulePayment, ValueType: constants.SystemConfigTypeJSON, DefaultValue: `["wallet","wechat","alipay"]`, Description: "卡资产允许的C端支付方式", Control: "payment_methods", Validator: paymentmethod.ValidateConfigValue},
		{Key: constants.SystemConfigPaymentAllowedDevice, Module: constants.SystemConfigModulePayment, ValueType: constants.SystemConfigTypeJSON, DefaultValue: `["wallet","wechat","alipay"]`, Description: "设备资产允许的C端支付方式", Control: "payment_methods", Validator: paymentmethod.ValidateConfigValue},
	}
	for _, definition := range definitions {
		if existing, exists := registry.Get(definition.Key); exists {
			if existing.ValueType != definition.ValueType || existing.Module != definition.Module {
				logPaymentMethodConfigRegistrationError(logger, definition.Key, "配置 Key 已被其他类型或模块注册")
			}
			continue
		}
		if err := registry.Register(definition); err != nil {
			logPaymentMethodConfigRegistrationError(logger, definition.Key, err.Error())
		}
	}
}

func logPaymentMethodConfigRegistrationError(logger *zap.Logger, key, reason string) {
	if logger != nil {
		logger.Error("注册支付方式系统配置失败，C端支付将失败关闭", zap.String("config_key", key), zap.String("reason", reason))
	}
}
