<?php

namespace App\Models;

use CodeIgniter\Model;

class SettingModel extends Model
{
    protected $table      = 'settings';
    protected $primaryKey = 'id';
    protected $returnType = 'array';

    protected $allowedFields = ['key', 'value'];

    public function getValue(string $key, string $default = ''): string
    {
        $row = $this->where('key', $key)->first();
        return $row ? (string) $row['value'] : $default;
    }

    public function setValue(string $key, string $value): void
    {
        $row = $this->where('key', $key)->first();
        if ($row) {
            $this->where('key', $key)->set('value', $value)->update();
        } else {
            $this->insert(['key' => $key, 'value' => $value]);
        }
    }
}
