package config

import (
	"io/ioutil"

	yaml "gopkg.in/yaml.v2"
)

const (
	LOG_LEVEL_OFF = iota
	LOG_LEVEL_INFO
	LOG_LEVEL_DEBUG
)

type ConfigT struct {

	// Domain

	Domain string

	// Logging

	LogLevel int `yaml:"log_level"`

	// Secret keys

	Keys struct {
		CookieStoreKey string `yaml:"cookie_store_key"`
		JWTSigningKey  string `yaml:"jwt_signing_key"`
	}

	// Admin credentials

	Admin struct {
		Username string
		Password string
	}

	// Database

	Orm struct {
		Connection  string
		Options     string
		Reset       bool
		AutoMigrate bool
		Regenerate  bool
	}
}

var Config *ConfigT

func init() {
	Config = new(ConfigT)
}

// ReadFile reads the config file placed at the given path.
func ReadFile(path string, config *ConfigT) error {
	cf, err := ioutil.ReadFile(path)
	if err != nil {
		return err
	}
	if err := yaml.Unmarshal(cf, config); err != nil {
		return err
	}
	return nil
}

// Read reads the config data from the given slice.
func Read(data []byte, config *ConfigT) error {
	if err := yaml.Unmarshal(data, config); err != nil {
		return err
	}
	return nil
}
