<?php
namespace App\Core;

/** Konfigurasi statis dari berkas config/config.php */
class Config
{
    private static array $data = [];
    private static bool $loaded = false;

    public static function load(string $file): bool
    {
        if (!is_file($file)) return false;
        $cfg = require $file;
        if (!is_array($cfg)) return false;
        self::$data = $cfg;
        self::$loaded = true;
        return true;
    }

    public static function loaded(): bool { return self::$loaded; }

    public static function get(string $key, $default = null)
    {
        $parts = explode('.', $key);
        $cur = self::$data;
        foreach ($parts as $p) {
            if (!is_array($cur) || !array_key_exists($p, $cur)) return $default;
            $cur = $cur[$p];
        }
        return $cur;
    }

    public static function set(string $key, $value): void
    {
        $parts = explode('.', $key);
        $ref = &self::$data;
        foreach ($parts as $p) {
            if (!isset($ref[$p]) || !is_array($ref[$p])) $ref[$p] = [];
            $ref = &$ref[$p];
        }
        $ref = $value;
    }

    public static function all(): array { return self::$data; }
}
