<?php

namespace App\Models;

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

class QuestionBankItem extends Model
{
    protected $fillable = [
        'question_bank_section_id',
        'question_en',
        'question_ar',
        'type',
        'options_en',
        'options_ar',
        'rating_min',
        'rating_max',
        'is_required',
        'placeholder_en',
        'placeholder_ar',
        'usage_count',
        'user_id',
    ];

    protected $casts = [
        'options_en'   => 'array',
        'options_ar'   => 'array',
        'is_required'  => 'boolean',
        'rating_min'   => 'integer',
        'rating_max'   => 'integer',
        'usage_count'  => 'integer',
    ];

    // ── Locale-aware helpers ───────────────────────────────────
    public function getQuestionAttribute(): string
    {
        return app()->getLocale() === 'ar'
            ? ($this->question_ar ?? $this->question_en)
            : $this->question_en;
    }

    public function getOptionsAttribute(): ?array
    {
        return app()->getLocale() === 'ar'
            ? ($this->options_ar ?? $this->options_en)
            : $this->options_en;
    }

    public function getPlaceholderAttribute(): ?string
    {
        return app()->getLocale() === 'ar'
            ? ($this->placeholder_ar ?? $this->placeholder_en)
            : $this->placeholder_en;
    }

    // ── Relationships ──────────────────────────────────────────
    public function section(): BelongsTo
    {
        return $this->belongsTo(QuestionBankSection::class, 'question_bank_section_id');
    }
}
