<?php

namespace App\Models\System;

use App\Traits\ModelTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Config extends Model
{
    use ModelTrait, SoftDeletes;

    protected $table = 'system_setting';

    protected $fillable = [
        'group',
        'key',
        'name',
        'type',
        'options',
        'value',
        'default_value',
        'description',
        'validation',
        'sort',
        'is_system',
        'status',
    ];

    protected $casts = [
        'options' => 'array',
        'is_system' => 'boolean',
        'status' => 'boolean',
    ];

    public function getOptionsAttribute($value)
    {
        if (is_string($value)) {
            return json_decode($value, true) ?? [];
        }
        return $value;
    }

    public function setOptionsAttribute($value)
    {
        $this->attributes['options'] = is_array($value) ? json_encode($value) : $value;
    }
}
