package env

import (
	"os"
	"strconv"
	"strings"
	"time"
)

func GetString(key, fallback string) string {
	val, ok := os.LookupEnv(key)
	if !ok {
		return fallback
	}
	return val
}

func GetInt(key string, fallback int) int {
	val, ok := os.LookupEnv(key)
	if !ok {
		return fallback
	}
	valAsInt, err := strconv.Atoi(val)
	if err != nil {
		return fallback
	}
	return valAsInt
}

func GetDuration(key string, fallback string) time.Duration {
	parsedFallback, _ := time.ParseDuration(fallback)

	val, ok := os.LookupEnv(key)
	if !ok {
		return parsedFallback
	}

	parsedVal, err := time.ParseDuration(val)
	if err != nil {
		return parsedFallback
	}

	return parsedVal
}

func GetBool(key string, fallback bool) bool {
	val, ok := os.LookupEnv(key)
	if !ok {
		return fallback
	}
	valAsBool, err := strconv.ParseBool(val)
	if err != nil {
		return fallback
	}
	return valAsBool
}

func GetList(key string, fallback []string) []string {
	val, ok := os.LookupEnv(key)
	if !ok {
		return fallback
	}
	return strings.Split(val, ",")
}
