package config

// Config holds the application configuration
type Config struct {
	Env          string                 `mapstructure:"env"`
	Day          string                 `mapstructure:"day"`
	InputDir     string                 `mapstructure:"inputDir"`
	TikvInputDir string                 `mapstructure:"TikvInputDir"`
	ResultDir    string                 `mapstructure:"resultDir"`
	LogDir       string                 `mapstructure:"logDir"`
	Feishu       FeishuConfig           `mapstructure:"feishu"`
	Database     Database               `mapstructure:"database"`
	Queries      map[string]QueryConfig `mapstructure:"queries"`
	S3           S3Config               `mapstructure:"s3"`
	Parser       ParserConfig           `mapstructure:"parser"`
	Clinic       ClinicConfig           `mapstructure:"clinic"`
	O11yConfig   O11yConfig             `mapstructure:"o11y"`
	CommonSql    CommonSql              `mapstructure:"commonSql"`
	QueryProxy   QueryProxyConfig       `mapstructure:"queryProxy"`
}
type O11yConfig struct {
	Auth0TokenUrl     string `mapstructure:"auth0TokenUrl"`
	Auth0ClientId     string `mapstructure:"auth0ClientId"`
	Auth0ClientSecret string `mapstructure:"auth0ClientSecret"`
	Auth0Audience     string `mapstructure:"auth0Audience"`
	O11yMetadataBase  string `mapstructure:"o11yMetadataBase"`
}

// S3Config holds the S3 configuration
type S3Config struct {
	Region          string `mapstructure:"region"`
	Bucket          string `mapstructure:"bucket"`
	Prefix          string `mapstructure:"prefix"`
	AccessKeyID     string `mapstructure:"access_key_id"`
	SecretAccessKey string `mapstructure:"secret_access_key"`
	SessionToken    string `mapstructure:"session_token"`
	DisableSSL      bool   `mapstructure:"disable_ssl"`
	Endpoint        string `mapstructure:"endpoint"`
	ForcePathStyle  bool   `mapstructure:"force_path_style"`
	GithubToken     string `mapstructure:"github_token"`
	AuthURL         string `mapstructure:"auth_url"`
}

// CommonSql holds common SQL queries
type CommonSql struct {
	CountSql string `mapstructure:"countSql"`
}

// ParserConfig holds the parser configuration
type ParserConfig struct {
	BatchSize        int          `mapstructure:"batch_size"`
	Concurrency      int          `mapstructure:"concurrency"`
	SupportedFormats []string     `mapstructure:"supported_formats"`
	Filters          FilterConfig `mapstructure:"filters"`
}

// FilterConfig holds the filter configuration
type FilterConfig struct {
	MemThresholdGB            int `mapstructure:"mem_threshold_gb"`
	QueryTimeThresholdSeconds int `mapstructure:"query_time_threshold_seconds"`
}

// QueryConfig holds the configuration for a single query
type QueryConfig struct {
	SQL         string `mapstructure:"sql"`
	CountSql    string `mapstructure:"countSql"`
	Level       string `mapstructure:"level"`
	Order       int    `mapstructure:"order"`
	PatternName string `mapstructure:"patternName"`
}

// Database holds the database configuration
type Database struct {
	DbPath      string      `mapstructure:"dbPath"`
	PatternName string      `mapstructure:"patternName"`
	TikvDbPath  string      `mapstructure:"tikvDbPath"`
	MySQL       MySQLConfig `mapstructure:"mysql"`
}

// MySQLConfig holds the MySQL database configuration
type MySQLConfig struct {
	Host     string `mapstructure:"host"`
	Port     int    `mapstructure:"port"`
	User     string `mapstructure:"user"`
	Password string `mapstructure:"password"`
	Database string `mapstructure:"database"`
	Charset  string `mapstructure:"charset"`
}

// QueryProxyConfig holds upstream query proxy configuration
type QueryProxyConfig struct {
	Host string `mapstructure:"host"`
	Path string `mapstructure:"path"`
}

// ClinicConfig holds the TiDB Clinic dashboard configuration
type ClinicConfig struct {
	BaseUrl                        string              `mapstructure:"baseUrl"`
	WebHost                        string              `mapstructure:"webHost"`
	O11yGlobalServerUrl            string              `mapstructure:"o11yGlobalServerUrl"`
	ClusterMetricsTemplateUrl      string              `mapstructure:"clusterMetricsTemplateUrl"`
	ClusterMetricsTemplateUrlTiDBX string              `mapstructure:"clusterMetricsTemplateUrlTiDBX"`
	ClinicInternalBaseURL          string              `mapstructure:"clinicInternalBaseURL"`
	MetricsMapping                 MetricsMappingConfig `mapstructure:"metricsMapping"`
}

// MetricsMappingConfig holds the metrics category mapping configuration
// DataPlaneMetrics is a blacklist - metrics in this list use DataPlane endpoint, all others use ClusterMetrics
type MetricsMappingConfig struct {
	DataPlaneMetrics []string `mapstructure:"dataPlaneMetrics" yaml:"data_plane_metrics"`
}

// FeishuConfig holds the Feishu configuration
type FeishuConfig struct {
	DocumentID   string             `mapstructure:"documentID"`
	ReceiveID    string             `mapstructure:"receiveID"`
	AppID        string             `mapstructure:"appID"`
	AppSecret    string             `mapstructure:"appSecret"`
	DocumentUrl  string             `mapstructure:"documentUrl"`
	CardTemplate CardTemplateConfig `mapstructure:"cardTemplate"`
}

// CardTemplateConfig Store Feishu card template related configuration
type CardTemplateConfig struct {
	TemplateID          string `mapstructure:"templateID"`
	TemplateVersionName string `mapstructure:"templateVersionName"`
}
