<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
class Setting extends Model {
    protected $guarded = ['id'];
    public $timestamps = false;
    public static function get(string $key, $default = null) {
        $row = static::where('key', $key)->first();
        if (!$row) return $default;
        return match ($row->type) {
            'boolean' => (bool) $row->value, 'number' => $row->value + 0,
            'json' => json_decode((string) $row->value, true), default => $row->value,
        };
    }
    public static function put(string $key, $value, string $group = 'general', string $type = 'string', ?string $label = null): void {
        if ($type === 'json' || is_array($value)) { $value = json_encode($value); $type = 'json'; }
        static::updateOrCreate(['key' => $key], ['value' => $value, 'group' => $group, 'type' => $type, 'label' => $label ?? $key]);
    }
}
