package infrastructure

import (
	"github.com/creasty/defaults"
	"github.com/spf13/viper"
)

type Config struct {
	DBUser        string `mapstructure:"db-user" default:"userpg"`
	DBName        string `mapstructure:"db-name" default:"58blog"`
	DBPass        string `mapstructure:"db-password" default:"1205"`
	DBHost        string `mapstructure:"db-host" default:"localhost"`
	DBPort        string `mapstructure:"db-port" default:"5432"`
	AdminName     string `mapstructure:"admin_name" default:"muts"`
	AdminPassword string `mapstructure:"admin_pass" default:"1205"`
	ImagesPath    string `mapstructure:"images_path" default:"./images/"`
	PostsPath     string `mapstructure:"posts_path" default:"./posts/"`
}

func LoadConfig() (config *Config, err error) {
	config = &Config{}
	if err = defaults.Set(config); err != nil {
		return nil, err
	}

	viper.SetConfigName("config")
	viper.SetConfigType("yaml")
	viper.AddConfigPath(".")
	viper.AddConfigPath("/etc/58team_blog/")
	viper.AddConfigPath("/58team_blog/cfgs/")

	if err = viper.ReadInConfig(); err != nil {
		return nil, err
	}

	if err = viper.Unmarshal(&config); err != nil {
		return nil, err
	}

	return config, nil
}
