package server

import (
	"github.com/UpmeshLTD/urus-aio/infrastructure/config"
	"github.com/UpmeshLTD/urus-aio/infrastructure/config/registry"
	"github.com/spf13/viper"
)

// ConfigName is the logging configuration name
const ConfigName = "server"

// ConfigSectionName is the name of the section used to query configuration information.
//
// To override this name, change it to desired value using server.ConfigSectionName = "some_name".
var ConfigSectionName = ConfigName

type Config struct {
	// StartUpTimeoutMs is the time duration, in milliseconds, that the server waits for all transports to confirm
	// their starting up.
	StartUpTimeoutMs int

	// ShutDownTimeoutMs is the time duration, in milliseconds, that the server waits for all transports to shut down.
	ShutDownTimeoutMs int
}

func GetConfig(cp config.Provider) *Config {
	return cp.Get(ConfigName).(*Config)
}

func init() {
	registry.RegisterConfig(ConfigName, registry.NewConfig(func(v *viper.Viper) interface{} {
		return &Config{
			StartUpTimeoutMs:  v.GetInt(ConfigSectionName + ".start_up_timeout_ms"),
			ShutDownTimeoutMs: v.GetInt(ConfigSectionName + ".shut_down_timeout_ms"),
		}
	}, registry.WithSetDefault(func(v *viper.Viper) {
		v.SetDefault(ConfigSectionName+".start_up_timeout_ms", 500)
		v.SetDefault(ConfigSectionName+".shut_down_timeout_ms", 5000)
	})))
}
