<?php

namespace App\Models;

use CodeIgniter\Model;

class SystemSettingModel extends Model
{
    protected $table            = 'system_settings';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;

    protected $allowedFields    = ['key_name', 'key_value'];

    public function getSetting($key_name)
    {
        $result = $this->where('key_name', $key_name)->first();
        return $result ? $result['key_value'] : null;
    }

    public function setSetting($key_name, $key_value)
    {
        $existing = $this->where('key_name', $key_name)->first();
        if ($existing) {
            return $this->update($existing['id'], ['key_value' => $key_value]);
        } else {
            return $this->insert(['key_name' => $key_name, 'key_value' => $key_value]);
        }
    }
}
