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

package config

// Database configuration
type CentralConfig struct {
	ServerConf     ServerConfig     `mapstructure:"server"`
	SentryConf     SentryConfig     `mapstructure:"sentry"`
	MysqlConf      MysqlConfig      `mapstructure:"mysql"`
	GrpcConf       GrpcConfig       `mapstructure:"grpc"`
	MessageSvcConf MessageSvcConfig `mapstructure:"messagesvc"`
	QueryLogConf   QueryLogConfig   `mapstructure:"querylog"`
}

type MessageSvcConfig struct {
	OpenAIEndpoint    string `mapstructure:"openaiEndpoint" default:"https://api.openai.com"`
	OpenAIApiKey      string `mapstructure:"openaiApiKey" required:"true"`
	OpenAIModel       string `mapstructure:"openaiModel" default:"text-davinci-003"`
	SqlEditorEndpoint string `mapstructure:"sqlEditorEndpoint" default:"https://us-west-2.nightly.shared.aws.tidbcloud.com"`
	MaxRetryCount     int    `mapstructure:"maxRetryCount" default:"3"`
	MaxDelaySeconds   int    `mapstructure:"maxDelaySeconds" default:"40"`
}

type ServerConfig struct {
	LogLevel                   string         `mapstructure:"logLevel" default:"info"`
	GrpcPort                   string         `mapstructure:"grpcPort" default:"10000"`
	HttpPort                   string         `mapstructure:"httpPort" default:"10010"`
	MetricsPort                string         `mapstructure:"metricsPort" default:"9090"`
	LocalMode                  bool           `mapstructure:"localMode" default:"false"`
	EnableRateLimit            bool           `mapstructure:"enableRateLimit" default:"false"`
	OrgRateLimitPerDay         int            `mapstructure:"orgRateLimitPerDay" default:"100"`
	RateLimitWhitelist         map[uint64]int `mapstructure:"rateLimitWhitelist"`
	RunMigrate                 bool           `mapstructure:"runMigrate" default:"false"`
	Deployment                 string         `mapstructure:"deployment" default:"online"`
	OrgRateLimitOverSupportURL string         `mapstructure:"org_rate_limit_over_support_url" default:"https://support.pingcap.com/hc/en-us/requests/new?ticket_form_id=7800003722519"`
	HackyQuestionEnabled       bool           `mapstructure:"hackyQuestionEnabled" default:"false"`
	RedisURI                   string         `mapstructure:"redis_uri" default:"redis://redis-cache:6379/1"`
	EnableStreamMode           bool           `mapstructure:"enableStreamMode" default:"false"`
}

type GrpcConfig struct {
	AccountProviderSvcAddr string `mapstructure:"accountProviderSvcAddr" default:"account-provider-grpc:10000"`
	DataAPISvcAddr         string `mapstructure:"dataAPISvcAddr" default:"dataservice-dataapi-service:10000"`
	CoreSvcAddr            string `mapstructure:"coreSvcAddr" default:"tempo-proxy.tidb-management-service:10102"`
}

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

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

type QueryLogConfig struct {
	Table string `mapstructure:"table"  default:"bot_interact_record"`
}

var globalConf = &CentralConfig{}

func IsLocalMode() bool {
	return globalConf.ServerConf.LocalMode
}
