<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
 * @property int $id
 * @property string $name
 * @property string $module_data
 * @property string $status
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 *
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Registrar newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Registrar newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Registrar query()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Registrar whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Registrar whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Registrar whereModuleData($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Registrar whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Registrar whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Registrar whereUpdatedAt($value)
 *
 * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\RegistrarSetting> $settings
 * @property-read int|null $settings_count
 *
 * @mixin \Eloquent
 */
class Registrar extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'module_data',
        'status',
    ];

    protected $casts = [
        'module_data' => 'array',
    ];

    /**
     * Bu registrar'a ait ayarları getirir
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function settings()
    {
        return $this->hasMany(RegistrarSetting::class);
    }

    /**
     * Tüm ayarları key-value formatında döndürür
     *
     * @return array
     */
    public function getSettingsArray()
    {
        $settingsArray = [];
        foreach ($this->settings as $setting) {
            $settingsArray[$setting->setting] = $setting->value;
        }

        return $settingsArray;
    }

    /**
     * Bu registrar'ın aktif olup olmadığını kontrol eder
     *
     * @return bool
     */
    public function isActive()
    {
        return $this->status === 'active';
    }
}
