<?php

namespace App\Models;

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

class QuestionBankSection extends Model
{
    protected $fillable = [
        'name_en',
        'name_ar',
        'color',
        'sort_order',
        'user_id',
    ];

    protected $casts = [
        'sort_order' => 'integer',
    ];

    // ── Locale-aware name ──────────────────────────────────────
    public function getNameAttribute(): string
    {
        return app()->getLocale() === 'ar'
            ? ($this->name_ar ?? $this->name_en)
            : $this->name_en;
    }

    // ── Relationships ──────────────────────────────────────────
    public function items(): HasMany
    {
        return $this->hasMany(QuestionBankItem::class)
                    ->orderByDesc('usage_count');
    }
}
