<?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 City extends Model
{
    use HasFactory, HasTranslations, FilterableTrait;

    protected $fillable = [
        'country_id',
        'name',
        'is_active',
    ];

    public array $translatable = ['name'];

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

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

    /**
     * Relationship: Country
     */
    public function country()
    {
        return $this->belongsTo(Country::class);
    }

    /**
     * Relationship: Areas
     */
    public function areas()
    {
        return $this->hasMany(Area::class);
    }

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

    /**
     * Scope: By country
     */
    public function scopeByCountry($query, int $countryId)
    {
        return $query->where('country_id', $countryId);
    }
}
