<?php
namespace boru\ai\Config;

class Env
{
    /** @var bool */
    protected static $defaultsLoaded = false;

    public static function loadDefaultFiles($baseDir = null, $overrideExisting = false)
    {
        if (static::$defaultsLoaded && !$overrideExisting) {
            return;
        }

        $baseDir = $baseDir ? $baseDir : getcwd();
        if (!is_string($baseDir) || trim($baseDir) === '') {
            return;
        }

        Dotenv::load(array(
            rtrim($baseDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '.env',
            rtrim($baseDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '.env.local',
        ), $overrideExisting);

        if (!$overrideExisting) {
            static::$defaultsLoaded = true;
        }
    }

    public static function get($name, $default = null)
    {
        $value = getenv($name);
        if ($value === false || $value === null || $value === '') {
            return $default;
        }
        return $value;
    }
}
