package config

import (
	"github.com/UpmeshLTD/urus-aio/infrastructure/config"
	"github.com/spf13/viper"
)

type configImpl struct {
	viper        *viper.Viper
	systemConfig *SystemConfig
}

type SystemConfig struct {
	App     App     `mapstructure:",squash"`
	AWS     AWS     `mapstructure:",squash"`
	GRPC    GRPC    `mapstructure:",squash"`
	Cronjob Cronjob `mapstructure:",squash"`
}

//go:generate mockery --name=ISystemConfig --inpackage --case=snake
type Config interface {
	GetAppConfig() App
	GetAWSConfig() AWS
	GetGRPCConfig() GRPC
	GetCronjobConfig() Cronjob
}

func NewConfig() (Config, error) {
	cfg := &configImpl{
		viper: viper.New(),
	}
	systemCfg := &SystemConfig{}
	cfg.setDefaultConfig()
	res, err := config.LoadConfig(cfg.viper, systemCfg)
	if err != nil {
		return nil, err
	}
	return &configImpl{
		systemConfig: res.(*SystemConfig),
	}, nil
}

func (c *configImpl) setDefaultConfig() {
	c.setDefaultAppConfig()
	c.setDefaultAWSConfig()
	c.setDefaultGRPCConfig()
	c.setDefaultCronjobConfig()
}
