package config

import (
	"log"

	"github.com/joho/godotenv"
)

// AppConfig contains application configuration
var AppConfig Config

// Load application configuration
func Load() error {

	// load .env file
	err := godotenv.Load()

	if err != nil {
		log.Println(".env file not presented. Retrieving configuration from environment variables")
	}

	AppConfig = Config{

		Service: Service{
			// 9000 DEFAULT FOR DEV ENVIRONMENT
			Port:             getEnv("NOVATECH_SERVICE_PORT", "9000"),
			Environment:      getEnv("NOVATECH_SERVICE_ENVIRONMENT", "DEV"),
			BlockchainSecret: getEnv("NOVATECH_SERVICE_BLOCKCHAIN_SECRET", "novatech_service_blockchain_secret"),
		},
		AdminService: Service{
			// 8080 DEFAULT FOR DEV ENVIRONMENT
			Port:        getEnv("NOVATECH_ADMIN_SERVICE_PORT", "8080"),
			Environment: getEnv("NOVATECH_ADMIN_SERVICE_ENVIRONMENT", "DEV"),
		},
		Database: Database{
			UserName:     getEnv("NOVATECH_DATABASE_USERNAME", "root"),
			Password:     getEnv("NOVATECH_DATABASE_PASSWORD", "root"),
			DatabaseName: getEnv("NOVATECH_DATABASE_NAME", "postgres"),
			HostName:     getEnv("NOVATECH_DATABASE_ADDRESS", "localhost"),
			Port:         getEnv("NOVATECH_DATABASE_PORT", " "),
		},
		Blockchain: Blockchain{
			NetworkEndpoint:  getEnv("NOVATECH_BLOCKCHAIN_NETWORK_ENDPOINT", ""),
			ContractAddress:  getEnv("NOVATECH_BLOCKCHAIN_CONTRACT_ADDRESS", ""),
			WalletAddress:    getEnv("NOVATECH_BLOCKCHAIN_WALLET_ADDRESS", ""),
			WalletPrivateKey: getEnv("NOVATECH_BLOCKCHAIN_WALLET_PRIVATE_KEY", ""),
		},
	}

	return nil
}
