<?php

namespace KeyLair;

class Config
{
    private static ?array $data = null;

    private static function load(): array
    {
        if (self::$data === null) {
            $path = dirname(__DIR__) . '/config/config.php';

            if (!file_exists($path)) {
                http_response_code(500);
                die(
                    'Configuration file missing. Copy config/config.sample.php to ' .
                    'config/config.php and fill in your database credentials. ' .
                    'See install/INSTALL.md for the full setup guide.'
                );
            }

            self::$data = require $path;
        }

        return self::$data;
    }

    public static function get(string $section, $key = null)
    {
        $data = self::load();

        if (!isset($data[$section])) {
            return null;
        }

        if ($key === null) {
            return $data[$section];
        }

        return $data[$section][$key] ?? null;
    }
}
