<?php

namespace App\Models;

use App\Traits\BlameableTrait;
use App\Traits\FilterableTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Language extends Model
{
    use HasFactory, FilterableTrait, BlameableTrait;

    protected $fillable = [
        'code',
        'name',
        'native_name',
        'direction',
        'is_active',
        'is_default',
    ];

    protected function casts(): array
    {
        return [
            'is_active' => 'boolean',
            'is_default' => 'boolean',
        ];
    }

    // Filterable configuration
    protected array $filterable = ['code', 'name', 'is_active', 'is_default'];
    protected array $searchable = ['code', 'name', 'native_name'];
    protected array $sortable = ['id', 'code', 'name', 'created_at'];

    /**
     * Scope: Only active languages
     */
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    /**
     * Scope: Default language
     */
    public function scopeDefault($query)
    {
        return $query->where('is_default', true);
    }

    /**
     * Find language by code
     */
    public static function findByCode(string $code): ?self
    {
        return static::where('code', $code)->first();
    }

    /**
     * Check if language is RTL
     */
    public function isRtl(): bool
    {
        return $this->direction === 'rtl';
    }
}
