// Tool Url: https://github.com/jinzhu/configor

package config

var globalConf = &CentralConfig{}

type CentralConfig struct {
	GrpcConfig     GrpcConfig     `mapstructure:"grpc"`
	InfraAPIConfig InfraAPIConfig `mapstructure:"infraAPI"`
	ServerConfig   ServerConfig   `mapstructure:"server"`
	GardenerConfig GardenerConfig `mapstructure:"gardener"`
	MysqlConfig    MysqlConfig    `mapstructure:"mysql"`
	SentryConf     SentryConfig   `mapstructure:"sentry"`
}
type GrpcConfig struct {
	AccountProviderSvcAddr string `mapstructure:"accountProviderSvcAddr" default:"account-provider-grpc:10000"`
	DataAPISvcAddr         string `mapstructure:"dataAPISvcAddr" default:"dataservice-dataapi-service:10000"`
	MessageServiceAddr     string `mapstructure:"messageServiceAddr" default:"aipower-api-service:10000"`
	CoreSvcAddr            string `mapstructure:"coreSvcAddr" default:"dbaas-core-svc-server-grpc:8888"`
}

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

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

type ServerConfig struct {
	OverwriteDefault            string `mapstructure:"overwriteDefault" default:"no"` // Flag to specify whether `devtier` section is given in configure file
	HttpListenAddr              string `mapstructure:"httpListenAddr" default:":8080"`
	KubeConfigPath              string `mapstructure:"kubeConfigPath"`
	Chat2QueryApiHttpListenAddr string `mapstructure:"chat2queryapiHttpListenAddr" default:":8081"`
	Chat2QueryInternalAddr      string `mapstructure:"chat2queryInternalAddr" default:":8082"`
	GrpcPort                    string `mapstructure:"grpcPort" default:"10000"`
	EnableStreaming             bool   `mapstructure:"enableStreaming" default:"false"`
	GenQuestionConcurrently     bool   `mapstructure:"genQuestionConcurrently" default:"false"`
	LocalMode                   bool   `mapstructure:"localMode" default:"false"`
	LogLevel                    string `mapstructure:"logLevel" default:"info"`
	EDASvcAddr                  string `mapstructure:"edaAddr"`
}

type GardenerConfig struct {
	GcpMasterType  string `mapstructure:"gcpMasterType" default:"n1-standard-2"`
	GcpMasterCount int32  `mapstructure:"gcpMasterCount" default:"1"`
	AwsMasterType  string `mapstructure:"awsMasterType" default:"t3.medium"`
	AwsMasterCount int32  `mapstructure:"awsMasterCount" default:"1"`

	GardenerProjectName string `mapstructure:"gardenerProjectName" default:"core" required:"true"`
	// The base domain of gardener
	GardenBaseDomain string `mapstructure:"gardenBaseDomain" default:"garden.shared.aws.tidbcloud.com"`
	// kubeconfig file for base cluster, ignore if in cluster
	NodeImage        string `mapstructure:"nodeImage"`
	KubeConfigPath   string `mapstructure:"kubeConfigPath"`
	StorageNodeImage string `mapstructure:"storageNodeImage"`
	Env              string `mapstructure:"env"`
}

type MysqlConfig struct {
	Host     string `mapstructure:"host"  required:"true"`
	User     string `mapstructure:"user" default:"sqleditor"`
	Port     uint   `mapstructure:"port" default:"4000"`
	Password string `mapstructure:"password" required:"true"`
	Database string `mapstructure:"database" default:"sqleditor"`
	CharSet  string `mapstructure:"charset"  default:"utf8mb4"`
	TimeZone string `mapstructure:"timezone"  default:"UTC"`
}

type SentryConfig struct {
	DSN         string `mapstructure:"dsn"`
	Environment string `mapstructure:"env"`
	LogLevel    string `mapstructure:"logLevel" default:"warn"`
}

func (c *ServerConfig) IsGiven() bool {
	return c.OverwriteDefault == "no"
}

// NeedInit returns true if we need to initialize the infraapi API.
func (ic *InfraAPIConfig) NeedInit() bool {
	return true
}
