package app

import (
	"os"

	"gitlab.com/security-rd/go-pkg/logging"
	"gopkg.in/yaml.v2"
)

type Config struct {
	RegionConfigs []RegionConfig `yaml:"region_configs"`
}

type RegionConfig struct {
	RegionCode     string `yaml:"region_code"`
	ApiServer      string `yaml:"api_server"`
	CAData         string `yaml:"ca_data"`
	Token          string `yaml:"token"`
	ClientCertData string `yaml:"client_cert_data"`
	ClientKeyData  string `yaml:"client_key_data"`
}

func LoadConfig() *Config {
	configFile := "config.yaml"
	if envFile := os.Getenv("CONFIG_FILE"); envFile != "" {
		configFile = envFile
	}

	data, err := os.ReadFile(configFile)
	if err != nil {
		logging.Get().Error().Err(err).Msgf("Failed to read config file: %s", configFile)
		return nil
	}

	var config Config
	if err := yaml.Unmarshal(data, &config); err != nil {
		logging.Get().Error().Err(err).Msg("Failed to parse config file")
		return nil
	}

	return &config
}
