package common

import (
	"os"

	cnf "git.pbiernat.dev/egosport/identity-svc/pkg/config"
	srv "git.pbiernat.dev/egosport/identity-svc/pkg/server"
)

const (
	defAppName       = "api-svc"
	defAppDomain     = "api-svc"
	defCacheAddr     = "cache:6379"
	defCachePassword = "12345678"
	defDbURL         = "postgres://egosport:12345678@db.egosport:5432/egosport"
	defKVNmspc       = "egosport/api"
	defLoggerAddr    = "logger.egosport:24224"
	defNetAddr       = ":80"
	defPathPrefix    = "/api"
)

type Config struct {
	Base *srv.Config

	DbURL            string `json:"db_url"`
	CacheAddr        string `json:"cache_addr"`
	CachePassword    string `json:"cache_password"`
	EventBusExchange string `json:"eventbus_exchange"`
	EventBusQueue    string `json:"eventbus_queue"`
	EventBusURL      string `json:"eventbus_url"`
	LoggerAddr       string `json:"logger_addr"`
	KVNamespace      string
	RegistryAddr     string

	// Fields with JSON mappings are available through Consul KV storage
}

func NewConfig(name string) *Config {
	c := new(Config)
	c.Base = new(srv.Config)

	c.Base.AppID, _ = os.Hostname()
	c.Base.AppName = name
	c.Base.NetAddr = cnf.GetEnv("SERVER_ADDR", defNetAddr)
	c.Base.PathPrefix = cnf.GetEnv("APP_PATH_PREFIX", defPathPrefix)

	c.CacheAddr = cnf.GetEnv("CACHE_ADDR", defCacheAddr)
	c.CachePassword = cnf.GetEnv("CACHE_PASSWORD", defCachePassword)
	c.DbURL = cnf.GetEnv("DATABASE_URL", defDbURL)
	c.KVNamespace = cnf.GetEnv("APP_KV_NAMESPACE", defKVNmspc)
	c.LoggerAddr = cnf.GetEnv("LOGGER_ADDR", defLoggerAddr)
	// c.RegistryAddr = cnf.GetEnv("REGISTRY_ADDR", defRegistryAddr)

	return c
}
