<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Question extends Model
{
    use SoftDeletes;

    protected $table = 'questions';

    protected $fillable = ['chat_category_id', 'name', 'name_ar', 'status'];
    protected $translatable = ['name'];

    public function chatCategory(): BelongsTo
    {
        return $this->belongsTo(ChatCategory::class);
    }

    public function answers(): HasMany
    {
        return $this->hasMany(Answer::class);
    }

    public function getAttributes()
    {
        foreach($this->translatable as $attribute) {
            $locale = app()->getLocale();
            $localizedKey = "{$attribute}_{$locale}";
            if (isset($this->attributes[$localizedKey])) {
                $this->attributes[$attribute] =  (empty($this->attributes[$localizedKey]))?$this->attributes["{$attribute}"]:$this->attributes[$localizedKey];
            }
        }
        return $this->attributes;
    }
}