<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * Maps onto the EXISTING `site_settings` table.
 * Columns `key`, `value`, `group` are reserved words — always backticked by the
 * query grammar, so they work fine; we expose helpers to avoid raw access.
 */
class Setting extends Model
{
    protected $table = 'site_settings';

    public $timestamps = false;

    protected $fillable = ['key', 'value', 'group', 'label'];

    /** Read a setting value (mirrors PHP setting_get). */
    public static function get(string $key, $default = null)
    {
        $row = static::query()->where('key', $key)->first();
        return $row?->value ?? $default;
    }

    /** Upsert a setting value (mirrors PHP setting_set). */
    public static function put(string $key, $value, string $group = 'general'): void
    {
        static::query()->updateOrCreate(
            ['key' => $key],
            ['value' => $value, 'group' => $group],
        );
    }
}
