<?php

namespace App\Services;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class SettingsService
{
    private const CACHE_KEY = 'settings:all';

    private const CACHE_TTL = 3600;

    public static function getAll(): array
    {
        return Cache::remember(self::CACHE_KEY, self::CACHE_TTL, function () {
            return DB::table('settings')->pluck('value', 'key')->toArray();
        });
    }

    public static function get(string $key, mixed $default = null): mixed
    {
        $settings = self::getAll();

        return $settings[$key] ?? $default;
    }

    public static function clear(): void
    {
        Cache::forget(self::CACHE_KEY);
    }

    public static function clearAndRefresh(): array
    {
        self::clear();

        return self::getAll();
    }
}
