<?php

namespace App\Models;

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

class Country extends Model
{
    use HasFactory, HasTranslations, FilterableTrait, BlameableTrait;

    protected $fillable = [
        'code',
        'name',
        'phone_code',
        'is_active',
    ];

    public array $translatable = ['name'];

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

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

    /**
     * Relationship: Cities
     */
    public function cities()
    {
        return $this->hasMany(City::class);
    }

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