package config

import (
	"fmt"
	"github.com/spf13/viper"
	"time"
)

var GC Config

type Server struct {
	RunMode      string        `mapstructure:"run_mode"`
	HttpPort     int           `mapstructure:"http_port"`
	ReadTimeout  time.Duration `mapstructure:"read_timeout"`
	WriteTimeout time.Duration `mapstructure:"write_timeout"`
}

type Mysql struct {
	UserName string `mapstructure:"username"`
	PassWord string `mapstructure:"password"`
	Database string `mapstructure:"database"`
	Host     string `mapstructure:"host"`
}

type Log struct {
	Path       string `mapstructure:"path"`
	MaxSize    int    `mapstructure:"max_size"`
	MaxBackups int    `mapstructure:"max_backups"`
	MaxAge     int    `mapstructure:"max_age"`
	Compress   bool   `mapstructure:"compress"`
}

type Authing struct {
	AppId          string `mapstructure:"app_id"`
	AppSecret      string `mapstructure:"app_secret"`
	AppRedirectUri string `mapstructure:"app_redirect_uri"`
	AppLogOutUri   string `mapstructure:"app_logout_uri"`
	AppProviderUri string `mapstructure:"app_provider_uri"`
	UserPollId     string `mapstructure:"user_pool_id"`
	UserPollSecret string `mapstructure:"user_pool_secret"`
}

type Vendor struct {
	TencentAk   string `mapstructure:"tencent_ak"`
	TencentSk   string `mapstructure:"tencent_sk"`
	UcloudAk    string `mapstructure:"ucloud_ak"`
	UcloudSk    string `mapstructure:"ucloud_sk"`
	KsyAk       string `mapstructure:"ksy_ak"`
	KsySk       string `mapstructure:"ksy_sk"`
	GodaddyAk   string `mapstructure:"godaddy_ak"`
	GodaddySk   string `mapstructure:"godaddy_sk"`
	NamecheapAk string `mapstructure:"namecheap_ak"`
	ClientIp    string `mapstructure:"client_ip"`
	AliyunAk    string `mapstructure:"aliyun_ak"`
	AliyunSk    string `mapstructure:"aliyun_sk"`
	QiNiuAk     string `mapstructure:"qiniu_ak"`
	QiNiuSk     string `mapstructure:"qiniu_sk"`
	AwsAk       string `mapstructure:"aws_ak"`
	AwsSk       string `mapstructure:"aws_sk"`
	AwsSession  string `mapstructure:"aws_session"`
}

type Lark struct {
	OpenApiUri string `mapstructure:"open_api_uri"`
}

type Config struct {
	Server  *Server
	Mysql   *Mysql
	Log     *Log
	Vendor  *Vendor
	Authing *Authing
	Lark    *Lark
}

func LoadConfig() {
	viper.AddConfigPath("./config")
	viper.SetConfigName("config_prod")
	viper.SetConfigType("yaml")

	if err := viper.ReadInConfig(); err != nil {
		panic(fmt.Errorf("fatal error config file: %w", err))
	}

	err := viper.Unmarshal(&GC)
	if err != nil {
		panic(err)
	}
}
