package config

import (
	"github.com/tidbcloud/tiinf/tiinf"

	"github.com/tidbcloud/tidb-management-service/tidb-mgmt-service/infra/repository/db"
	"github.com/tidbcloud/tidb-management-service/utilities/featuregate"
)

type Config struct {
	AwsSecret           *AwsSecret           `yaml:"aws-secret"`
	DB                  *db.Config           `yaml:"db"`
	TiInfConfig         *tiinf.TiInfConfig   `yaml:"tiinf"`
	ServerRuntimeConfig *ServerRuntimeConfig `yaml:"server-runtime"`
	Workflow            *Workflow            `yaml:"workflow"`
	RootPasswordKMS     *RootPasswordKMS     `yaml:"root-password-kms"`
	AuditLog            *AuditLog            `yaml:"auditlog"`
	BizConfig           BizConfig            `yaml:"biz"`
	FeatureGates        map[string]featuregate.FeatureGate

	// Connection configs of infra/adapter.
	Serverless                 *ServerlessGlobalService          `yaml:"serverless"`
	Dedicated                  *DedicatedGlobalService           `yaml:"dedicated"`
	NextGen                    *NextGenGlobalService             `yaml:"nextgen"`
	Billing                    *BillingService                   `yaml:"billing"`
	DataFlow                   *DataFlowService                  `yaml:"dataflow"`
	Account                    *AccountService                   `yaml:"account"`
	CoreSvc                    *CoreSvcService                   `yaml:"coresvc"`
	EventBus                   *EventBus                         `yaml:"eventbus"`
	Jsm                        *Jsm                              `yaml:"jsm"`
	UserNotificationCenter     *UserNotificationCenter           `yaml:"user-notification-center"`
	ShadowPool                 *ShadowPoolConfig                 `yaml:"shadow-pool"`
	OneConsole                 *OneConsoleConfig                 `yaml:"one-console"`
	DedicatedToPremiumPrecheck *DedicatedToPremiumPrecheckConfig `yaml:"dedicated-to-premium-precheck"`

	// GlobalClusterServerAddr will be removed after migration is completed
	GlobalClusterServerAddr string `yaml:"global-cluster-server-addr"`
}

type Workflow struct {
	Mode string `yaml:"mode"`
}

type ServerRuntimeConfig struct {
	// This API key is a simple global-control-plane-singleton secret string
	//   that is used to check whether the caller can call our internal API and cluster proxy API.
	//   It is distributed by AWS Secret Manager.
	APIKey               string `yaml:"api-key"`
	DisableInternalCheck bool   `yaml:"disable-internal-check"`
}

type RootPasswordKMS struct {
	KeyID  string `yaml:"key-id"`
	KeyARN string `yaml:"key-arn"`
	Region string `yaml:"region"`
}

type AwsSecret struct {
	Key        string `yaml:"key"`
	Region     string `yaml:"region"`
	AwsProfile string `yaml:"aws-profile"`
	// AWS KMS customer master key (CMK) ARN.
	CMKArn string `yaml:"cmkArn" validate:"required"`
}

type ServerlessGlobalService struct {
	Url                   string `yaml:"url"`
	Local                 bool   `yaml:"local"`
	WaitTimeoutInMilliSec int    `yaml:"wait-timeout-in-milli-sec"`
	DataMigrationURL      string `yaml:"data-migration-url"`
}

type DedicatedGlobalService struct {
	Url                   string `yaml:"url"`
	Local                 bool   `yaml:"local"`
	WaitTimeoutInMilliSec int    `yaml:"wait-timeout-in-milli-sec"`
}

type NextGenGlobalService struct {
	Url                   string `yaml:"url"`
	Local                 bool   `yaml:"local"`
	WaitTimeoutInMilliSec int    `yaml:"wait-timeout-in-milli-sec"`
}

type DataFlowService struct {
	Url                   string `yaml:"url"`
	Local                 bool   `yaml:"local"`
	WaitTimeoutInMilliSec int    `yaml:"wait-timeout-in-milli-sec"`
}

type AccountService struct {
	Url                   string `yaml:"url"`
	Local                 bool   `yaml:"local"`
	WaitTimeoutInMilliSec int    `yaml:"wait-timeout-in-milli-sec"`
}

type BillingService struct {
	Billing2Url           string `yaml:"billing-2-url"` // [billing2-api.prod-ms:18888]
	Local                 bool   `yaml:"local"`
	WaitTimeoutInMilliSec int    `yaml:"wait-timeout-in-milli-sec"`
}

type AuditLog struct {
	Local bool `yaml:"local"`
}

type CoreSvcService struct {
	Url                   string `yaml:"url"`
	Local                 bool   `yaml:"local"`
	WaitTimeoutInMilliSec int    `yaml:"wait-timeout-in-milli-sec"`
}

type EventBus struct {
	// ProxyAddress represents which proxy should be used or not. such as "socks5://localhost:9092"
	ProxyAddress    string `yaml:"proxy-address"`
	RetryTimes      uint   `yaml:"retry-times"`
	NumOfGoroutines uint   `yaml:"num-of-goroutines"`
	Local           bool   `yaml:"local"`
}

type Jsm struct {
	Enable    bool   `yaml:"enable"`
	SqsRegion string `yaml:"sqs-region"`
	SqsUrl    string `yaml:"sqs-url"`
	Local     bool   `yaml:"local"`
}

type UserNotificationCenter struct {
	// Currently only support configs for some UNC API fields.
	// Types of event and payload is hard-coded.
	UNCSendingConfigs []*UNCSendingConfig `yaml:"unc-sending-configs"`
}

type UNCSendingConfig struct {
	TriggerID  string `yaml:"trigger-id"`
	WorkFlowID string `yaml:"workflow-id"`
	IsTest     bool   `yaml:"is-test"`
}

type BizConfig struct {
	ServerlessFreeQuotaMaxCount int `yaml:"serverlessFreeQuotaMaxCount"`
}

type ShadowPoolConfig struct {
	FakeUserID    string            `yaml:"fake-user-id"`
	CreatedBy     string            `yaml:"created-by"`
	PoolIDs       []uint64          `yaml:"pool-ids"`
	SpecificCodes map[string]string `yaml:"specific-codes"`
	// ClaimUrlHost is the host used to build the claim URL for a zero instance.
	// e.g. dev.tidbcloud.com / staging.tidbcloud.com / tidbcloud.com
	ClaimUrlHost string `yaml:"claim-url-host"`
}

type OneConsoleConfig struct {
	CheckXProjectType bool `yaml:"check-x-project-type"`
}

type DedicatedToPremiumPrecheckConfig struct {
	SupportedChangefeedSinkTypes []string `yaml:"supported-changefeed-sink-types"`
}
