package config

import "github.com/tidbcloud/pkgv2/gormutil"

var (
	globalConf *ChaosConfig = &ChaosConfig{}
)

type ChaosConfig struct {
	ServerConfig ServerConfig    `mapstructure:"server"`
	MysqlConfig  gormutil.Config `mapstructure:"mysql"`
}

type ServerConfig struct {
	GrpcAddr       string           `mapstructure:"grpcAddr" default:"0.0.0.0:8888"`
	HttpAddr       string           `mapstructure:"httpAddr" default:"0.0.0.0:8080"`
	LogLevel       string           `mapstructure:"logLevel" default:"info" validate:"oneof=debug info warn error fatal"`
	Stage          string           `mapstructure:"stage" validate:"required"`
	SecretStore    *SecretConfig    `mapstructure:"secret"`
	InfraAPIConfig InfraAPIConfig   `mapstructure:"infraAPI"`
	SvcAddresses   ServiceAddresses `mapstructure:"serviceAddresses"`
	GardenerConfig GardenerConfig   `mapstructure:"gardener"`
}

type ServiceAddresses struct {
	CoreSvcAddr string `mapstructure:"coreSvcAddr" default:"dbaas-core-svc-server-grpc:10000"`
}

// InfraAPIConfig is the configuration items for infra API operations.
type InfraAPIConfig struct {
	// kubeconfig file for the infra API server, ignore this to use the default secret
	KubeConfigPath string `mapstructure:"kubeConfigPath"`

	// ref https://github.com/tidbcloud/infra-api/pull/79
	// Enable this too keep the 'key' of resources do not change in infra-api.
	// will use projectID@dbaas as projectID@infra-api
	ProjectIDAsInfraProjectID bool `mapstructure:"projectIDAsInfraProjectID"`
}

type SecretConfig struct {
	SecretKey string `mapstructure:"secretKey"`
	Region    string `mapstructure:"region"`
}

type GardenerConfig struct {
	GardenerProjectName string `mapstructure:"gardenerProjectName" default:"core" required:"true"`
}

func Global() *ChaosConfig {
	return globalConf
}
