package config

import (
	"fmt"

	"github.com/ilyakaznacheev/cleanenv"
)

type Host struct {
	Hostname string   `yaml:"hostname"`
	Ip       string   `yaml:"ip"`
	Roles    []string `yaml:"roles"`
	User     string   `yaml:"user"`
	Password string   `yaml:"password"`
}

type Config struct {
	WorkDir        string
	KubeconfigFile string `yaml:"kubeconfig_file" env-default:"k8s-admin.conf"`
	Verbose        bool
	Credentials    struct {
		User            string `yaml:"user"`
		Password        string `yaml:"password"`
		AskSudoPassword bool   `yaml:"ask_sudo_password"`
		PrivateKeyFile  string `yaml:"private_key_file"`
	} `yaml:"credentials"`

	Hosts []Host `yaml:"hosts"`

	Orchestrator Orchestrator `yaml:"orchestrator"`

	Modules struct {
		Additional Additional `yaml:"additional"`
	} `yaml:"modules"`
}

var instance *Config

func CreateConfig(configPath string, workDir string, password string) *Config {
	instance = &Config{}
	instance.WorkDir = workDir

	if err := cleanenv.ReadConfig(configPath, instance); err != nil {
		helper, _ := cleanenv.GetDescription(instance, nil)
		panic(fmt.Sprintf("%s\n%s", helper, err))
	}

	if password != "" {
		instance.Credentials.Password = password
	}
	return instance
}

func GetConfig() *Config {
	return instance
}
