<?php

namespace App\Models;

use CodeIgniter\Model;

class SettingModel extends Model
{
    protected $table            = 'setting';
    protected $primaryKey       = 'key';
    protected $allowedFields    = ['key', 'value'];
    public    $useAutoIncrement = false;
    protected $returnType       = 'array';

    // Ambil nilai setting berdasarkan key
    public function get($key)
    {
        $row = $this->where('key', $key)->first();
        return $row ? $row['value'] : null;
    }

    // Simpan atau update setting
    public function saveSetting($key, $value)
    {
        return $this->db->table($this->table)->replace([
            'key' => $key,
            'value' => $value
        ]);
    }

    // Ambil setting receipt
    public function getReceiptSetting()
    {
        $raw = $this->get('receipt');
        return $raw ? json_decode($raw, true) : ['header' => '', 'footer' => '', 'logo' => ''];
    }
}
