<?php
namespace EasyChat\Library\Support;

use EasyChat\Library\RequestContext;








final class OfficialSupportRuntimeConfig {
    public static function value(string $name): string {
        if (preg_match('/^EASYCHAT_[A-Z0-9_]{1,96}$/D', $name) !== 1) {
            return '';
        }

        $environment = getenv($name);
        if (is_string($environment) && $environment !== '') {
            return trim($environment);
        }

        $server = class_exists(RequestContext::class)
            ? RequestContext::server()
            : (isset($GLOBALS['_SERVER']) && is_array($GLOBALS['_SERVER']) ? $GLOBALS['_SERVER'] : []);
        foreach ([$name, 'REDIRECT_' . $name, 'REDIRECT_REDIRECT_' . $name] as $candidate) {
            if (!isset($server[$candidate]) || !is_scalar($server[$candidate])) {
                continue;
            }
            $value = trim((string)$server[$candidate]);
            if ($value !== '') {
                return $value;
            }
        }

        return '';
    }

    public static function enabled(string $name, bool $fallback = false): bool {
        $value = strtolower(self::value($name));
        if ($value === '') {
            return $fallback;
        }
        return in_array($value, ['1', 'true', 'yes', 'on'], true);
    }
}
