package common

import (
	"os"

	cnf "git.pbiernat.dev/egommerce/identity-service/pkg/config"
	srv "git.pbiernat.dev/egommerce/identity-service/pkg/server"
)

const (
	defAppName          = "identity-svc"
	defAppDomain        = "identity-svc"
	defPathPrefix       = "/identity"
	defNetAddr          = ":80"
	defLoggerAddr       = "api-logger:24224"
	defRegistryAddr     = "api-registry:8500"
	defDbURL            = "postgres://postgres:12345678@postgres-db:5432/egommerce"
	defCacheAddr        = "api-cache:6379"
	defCachePassword    = "12345678"
	defMongoDbURL       = "mongodb://mongodb:12345678@mongo-db:27017"
	defEventBusURL      = "amqp://guest:guest@api-gateway:5672"
	defEbEventsExchange = "api-events"
	defEbEventsQueue    = "identity-svc"
	defKVNmspc          = "dev.egommerce/service/identity-svc"
)

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.EventBusExchange = defEbEventsExchange
	c.EventBusURL = cnf.GetEnv("EVENTBUS_URL", defEventBusURL)
	c.KVNamespace = cnf.GetEnv("APP_KV_NAMESPACE", defKVNmspc)
	c.LoggerAddr = cnf.GetEnv("LOGGER_ADDR", defLoggerAddr)
	c.RegistryAddr = cnf.GetEnv("REGISTRY_ADDR", defRegistryAddr)

	return c
}
