package logger

import (
	"LAPP_ACURA_MOM_BACKEND/utils"
	"errors"
	"github.com/spf13/viper"
)

var loggerConfig = new(outputSettings)
var outputConfig = new(output)

type output struct {
	DB      map[string]db   `yaml:"db"`
	File    map[string]file `yaml:"file"`
	Console console         `yaml:"console"`
}

type db struct {
	DBType   string `yaml:"dbtype"`
	Host     string `yaml:"host"`
	Port     int    `yaml:"port"`
	Database string `yaml:"database"`
	Table    string `yaml:"table"`
	User     string `yaml:"user"`
	Pwd      string `yaml:"pwd"`
}

type file struct {
	Filename string `yaml:"filename"`
	//MaxSize    int    `yaml:"maxSize"`
	MaxAge int `yaml:"maxAge"`
	//MaxBackups int    `yaml:"maxBackups"`
	//Compress   bool   `yaml:"compress"`
}

type console struct {
	Enable bool `yaml:"enable"`
}

type outputDriver struct {
	DriverType string `yaml:"drivertype"`
	Level      string `yaml:"level"`
	DriverName string `yaml:"drivername"`
}

type outputSettings struct {
	Config map[string]map[string]outputDriver `yaml:"config"`
}

func InitConfig() error {
	var err error
	baseDir, err := utils.GetCurrentPath("conf")
	if err != nil {
		return err
	}
	viper.SetConfigName("log_config")
	viper.SetConfigType("yaml")
	viper.AddConfigPath(baseDir)
	if err = viper.ReadInConfig(); err != nil {
		return err
	}
	if err = viper.Unmarshal(loggerConfig); err != nil {
		return err
	}
	if err = viper.Unmarshal(outputConfig); err != nil {
		return err
	}
	_, exist := loggerConfig.Config["root"]
	if !exist {
		return errors.New("未配置root默认配置")
	}
	return nil
}
