package config

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

	"github.com/tidbcloud/tidb-management-service/utilities/featuregate"
)

var globalConf = &Config{}

type Config struct {
	MysqlConfig gormutil.Config `mapstructure:"mysql"`

	CoreSvcSecretStore *SecretConfig `mapstructure:"core-svc-secret"`

	InfraConfig InfraConfig `mapstructure:"infra"`

	ServerConfig ServerConfig `mapstructure:"server"`

	ProxyConfig ProxyConfig `mapstructure:"proxy"`

	AwsConfig AwsConfig `mapstructure:"aws"`

	Eventbus Eventbus `mapstructure:"eventbus"`

	RegionalServiceConfig RegionalServiceConfig `mapstructure:"regionalserver" validate:"required"`
}

type Eventbus struct {
	ProxyAddress    string `mapstructure:"proxyAddress"`
	RetryTimes      uint   `mapstructure:"retryTimes"      default:"15"`
	NumOfGoroutines uint   `mapstructure:"numOfGoroutines" default:"5"`
}

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

type ProxyConfig struct {
	Auth0Domain       string `mapstructure:"auth0Domain"       validate:"required"`
	Auth0ClientID     string `mapstructure:"auth0ClientID"     validate:"required"`
	Auth0ClientSecret string `mapstructure:"auth0ClientSecret"`
	Auth0Audience     string `mapstructure:"auth0Audience"     validate:"required"`
}

type AwsConfig struct {
	PCAArn string `mapstructure:"pcaArn" validate:"required"`
}

type ServerConfig struct {
	SecretStore           *SecretConfig                      `mapstructure:"secret"`
	DependentSvcAddresses DependentSvcAddresses              `mapstructure:"dependentServiceAddresses"`
	FeatureGates          map[string]featuregate.FeatureGate `mapstructure:"featureGates"`
}

type DependentSvcAddresses struct {
	Billing2               string `mapstructure:"billing2"               default:"billing2:18888"              validate:"required"`
	Serverless             string `mapstructure:"serverlessSvcAddr"      default:"serverless-svc-grpc:10000"`
	AccountProviderSvcAddr string `mapstructure:"accountProviderSvcAddr" default:"account-provider-grpc:10000"`
	WaitTimeoutInMilliSec  int    `mapstructure:"waitTimeoutInMilliSec"  default:"8000"`
}

type InfraConfig struct {
	CMKArn string `mapstructure:"cmkArn" validate:"required"`
}

type RegionalServiceConfig struct {
	AllowRequestMaxClusterIdCount int                        `mapstructure:"allowRequestMaxClusterIdCount" default:"0"`
	ClientAuthKey                 string                     `mapstructure:"clientAuthKey"`
	RequestTimeoutInSec           int                        `mapstructure:"requestTimeoutInSec"                       validate:"required"`
	RetryCount                    int                        `mapstructure:"retryCount"                                validate:"required"`
	RetryWaitTimeoutInSec         int                        `mapstructure:"retryWaitTimeoutInSec"                     validate:"required"`
	RegionalProxyServiceConfig    RegionalProxyServiceConfig `mapstructure:"proxyConfig"`
}

type RegionalProxyServiceConfig struct {
	Addr                  string `mapstructure:"addr"                  default:"regional-proxy-grpc:10000"`
	WaitTimeoutInMilliSec int    `mapstructure:"waitTimeoutInMilliSec" default:"50"`
	Enabled               bool   `mapstructure:"enabled"               default:"false"`
}
