<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

final class Config extends Model
{
    /** The primary key column for config entries is a string key, not an auto-incrementing integer. */
    protected $primaryKey = 'key';

    /** Disable auto-incrementing since the primary key is a string. */
    public $incrementing = false;

    /** The primary key type is a string. */
    protected $keyType = 'string';

    /** Allow mass assignment on all columns. */
    protected $guarded = [];

    /**
     * Returns the attribute cast definitions for this model,
     * ensuring json_value is always hydrated as a PHP array.
     */
    protected function casts(): array
    {
        return [
            'json_value' => 'array',
        ];
    }

    /**
     * Scope to filter config records by their string key identifier.
     */
    public function scopeForKey(Builder $query, string $key): Builder
    {
        return $query->where('key', $key);
    }
}
