package libfeatures

import (
	"fmt"

	libConf "nms_nbi/lib/core/conf"
	libGlobal "nms_nbi/lib/global"
	libConfig "nms_nbi/restagent/config"

	"github.com/spf13/viper"
)

// BuildInfo 程序-V查看编译版本号信息
func BuildInfo() string {
	return fmt.Sprintf("OMC restagent version: %s\n%s\n%s\n\n", libGlobal.Version, libGlobal.BuildTime, libGlobal.GoVer)
}

// ConfigRead 指定配置文件读取
func ConfigRead(configFile string) {
	// 外层lib和features使用的配置
	libConfig.ReadConfig(configFile)
	uriPrefix := libConfig.GetYamlConfig().OMC.UriPrefix
	if uriPrefix != "" {
		libConfig.UriPrefix = uriPrefix
	}
	if libConfig.GetYamlConfig().TestConfig.Enabled {
		libConfig.ReadTestConfigYaml(libConfig.GetYamlConfig().TestConfig.File)
	}
	// 外层lib和features使用配置
	libConf.InitConfig(configFile)
}

// 配置文件读取进行内部参数合并
func ConfigInMerge() {
	// 合并外层lib和features使用配置
	for key, value := range libConf.AllSettings() {
		// 跳过配置
		if key == "testconfig" || key == "rest" || key == "logger" {
			continue
		}
		// 数据库配置
		if key == "database" {
			item := value.(map[string]any)
			defaultItem := viper.GetStringMap("gorm.datasource.default")
			defaultItem["host"] = item["host"]
			defaultItem["port"] = item["port"]
			defaultItem["username"] = item["user"]
			defaultItem["password"] = item["password"]
			defaultItem["database"] = item["name"]
			continue
		}
		viper.Set(key, value)
	}
}
