<?php

namespace App\Models;

use CodeIgniter\Model;

class SettingModel extends Model
{
    protected $table            = 'settings';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'object';
    protected $allowedFields    = ['key', 'value', 'created_at', 'updated_at'];
    protected $useTimestamps    = true;

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

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