<?php

final class Config
{
    private static array $data = [];
    private static array $settings = [];

    public static function load(string $file): void
    {
        if (!is_file($file)) {
            http_response_code(500);
            echo json_encode(['error' => 'api/config/config.php is missing. Copy config.sample.php and edit it.']);
            exit;
        }
        self::$data = require $file;
    }

    public static function get(string $key, $default = null)
    {
        return self::$data[$key] ?? $default;
    }

    /** Values from the settings table, cached per request. */
    public static function setting(string $key, ?string $default = null): ?string
    {
        if (!self::$settings) {
            foreach (DB::all('SELECT setting_key, setting_value FROM settings') as $r) {
                self::$settings[$r['setting_key']] = $r['setting_value'];
            }
        }
        return self::$settings[$key] ?? $default;
    }
}
