<?php
namespace Api\Core;

class Config {
    private $config;
    private $secrets;

    private function loadConfig() {
        if ($this->config === null) {
            $file = __DIR__ . '/../../../config/config.php';
            $this->config = file_exists($file) ? require $file : [];
        }
    }

    private function loadSecrets() {
        if ($this->secrets === null) {
            $file = __DIR__ . '/../../../config/secrets.php';
            $this->secrets = file_exists($file) ? require $file : [];
        }
    }

    public function get($key, $default = null) {
        $this->loadConfig();
        if (getenv(strtoupper($key)) !== false) {
            return getenv(strtoupper($key));
        }
        return $this->config[$key] ?? $default;
    }

    public function secret($key, $default = null) {
        $this->loadSecrets();
        if (getenv(strtoupper($key)) !== false) {
            return getenv(strtoupper($key));
        }
        return $this->secrets[$key] ?? $default;
    }
}
