package main

import (
	"encoding/json"
	"fmt"
	"os"
)

type Config struct {
	// database
	DBDriver   string `json:"db_driver"`
	DBDBName   string `json:"db_dbname"`
	DBHost     string `json:"db_host"`
	DBPort     string `json:"db_port"`
	DBUser     string `json:"db_user"`
	DBPassword string `json:"db_password"`
	DBOptions  string `json:"db_options"`

	// http client
	UserAgent string `json:"user_agent"`
	Delay     int    `json:"delay"`

	// auth
	AccessToken  string `json:"-"`
	RefreshToken string `json:"refresh_token"`
	// misc
	Debug bool `json:"debug"`
}

var _conf = Config{}

// LoadConfig reads a JSON config file and returns a populated Config.
func LoadConfig(path string) (*Config, error) {
	f, err := os.Open(path)
	if err != nil {
		return nil, fmt.Errorf("config: open %s: %w", path, err)
	}
	defer f.Close()

	c := &Config{}
	if err := json.NewDecoder(f).Decode(c); err != nil {
		return nil, fmt.Errorf("config: decode %s: %w", path, err)
	}
	return c, nil
}
