
<?php
function oldies_read_json(string $path, $default) {
    if (!file_exists($path)) return $default;
    $data = json_decode((string)file_get_contents($path), true);
    return is_array($data) || is_object($default) ? $data ?? $default : ($data === null ? $default : $data);
}
function oldies_write_json(string $path, $data): bool {
    $dir = dirname($path);
    if (!is_dir($dir)) @mkdir($dir, 0777, true);
    return file_put_contents($path, json_encode($data, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE)) !== false;
}
function oldies_h($s): string { return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); }
function oldies_redirect(string $url): void { header('Location: ' . $url); exit; }
function oldies_read_settings(): array {
    $file = __DIR__ . '/../data/settings.json';
    $defaults = [
        'chat_enabled'=>true,'chat_show_in_nav'=>true,'player_show_in_nav'=>true,'events_show_in_nav'=>true,
        'fans_show_in_nav'=>true,'team_show_in_nav'=>true,'listen_url'=>'http://cr3.broadcasturself.com:23990/stream',
        'stream_name'=>'Oldies And More Radio','popout_intro'=>'Keep the jukebox spinning in a dedicated player window.'
    ];
    $settings = oldies_read_json($file, []);
    return array_merge($defaults, is_array($settings) ? $settings : []);
}
function oldies_save_settings(array $settings): bool { return oldies_write_json(__DIR__ . '/../data/settings.json', $settings); }
