<?php

namespace App\Models;

use CodeIgniter\Model;

class SettingsModel extends Model
{
    protected $table            = 'settings';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'array';
    protected $protectFields    = true;
    protected $allowedFields    = ['setting_key', 'setting_value'];

    public function getSetting($key)
    {
        $setting = $this->where('setting_key', $key)->first();
        return $setting ? $setting['setting_value'] : null;
    }

    public function updateSetting($key, $value)
    {
        $exists = $this->where('setting_key', $key)->first();
        if ($exists) {
            return $this->where('setting_key', $key)->set(['setting_value' => $value])->update();
        } else {
            return $this->insert([
                'setting_key' => $key,
                'setting_value' => $value
            ]);
        }
    }

}
