package config

import (
	"time"

	mtime "go.unistack.org/micro/v4/util/time"
)

type AppConfig struct {
	CheckInterval mtime.Duration `json:"check_interval" yaml:"check_interval" default:"1d"`
}

type ServerConfig struct {
	Name        string `json:"name" yaml:"name"`
	Version     string `json:"-" yaml:"-"`
	Addr        string `json:"addr" yaml:"addr" default:":9090"`
	Crt         string `json:"crt" yaml:"crt"`
	Key         string `json:"key" yaml:"key"`
	ID          string `json:"-" yaml:"-" default:"micro:generate uuid"`
	LoggerLevel string `json:"logger_level" yaml:"logger_level"`
}

type TracerConfig struct {
	Metadata  map[string]string `json:"metadata" yaml:"metadata"`
	AgentHost string            `env:"JAEGER_AGENT_HOST" json:"host" yaml:"host" default:"127.0.0.1"`
	AgentPort string            `env:"JAEGER_AGENT_PORT" json:"port" yaml:"port" default:"6831"`
	Collector string            `env:"JAEGER_ENDPOINT,TRACER_ENDPOINT" json:"endpoint" yaml:"endpoint"`
}

type VaultConfig struct {
	Addr  string `env:"VAULT_ADDR" json:"addr" yaml:"addr" default:"http://127.0.0.1:8200"`
	Token string `env:"VAULT_TOKEN" json:"-" yaml:"-"`
	Path  string `env:"VAULT_PATH" json:"-" yaml:"-" default:"pkgdash/data/pkgdash"`
}

type MeterConfig struct {
	Addr string `json:"addr" yaml:"addr" default:"0.0.0.0:8080"`
	Path string `json:"path" yaml:"path" default:"/metrics"`
}

type DatabaseConfig struct {
	DSN             string        `json:"dsn" yaml:"dsn"`
	Type            string        `json:"-" yaml:"-"`
	Migrate         string        `json:"-" yaml:"-"`
	ConnStr         string        `json:"-" yaml:"-"`
	MaxOpenConns    int           `json:"-" yaml:"-"`
	MaxIdleConns    int           `json:"-" yaml:"-"`
	ConnMaxLifetime time.Duration `json:"-" yaml:"-"`
	ConnMaxIdleTime time.Duration `json:"-" yaml:"-"`
	MigrateForce    bool          `json:"-" yaml:"-"`
}

type Config struct {
	App      *AppConfig      `json:"app" yaml:"app"`
	Database *DatabaseConfig `json:"database" yaml:"database"`
	Server   *ServerConfig   `json:"server" yaml:"server"`
	Meter    *MeterConfig    `json:"meter" yaml:"meter"`
	Vault    *VaultConfig    `json:"-" yaml:"-"`
	Tracer   *TracerConfig   `json:"tracer" yaml:"tracer"`
}

func NewConfig(name, version string) *Config {
	return &Config{
		App:    &AppConfig{},
		Server: &ServerConfig{Name: name, Version: version},
		Tracer: &TracerConfig{},
		Meter:  &MeterConfig{},
		Vault:  &VaultConfig{},
	}
}
