<?php

namespace App\Models\LocalizationManagement;

use App\Models\Traits\Activable;
use Axn\EloquentAuthorable\AuthorableTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Nicolaslopezj\Searchable\SearchableTrait;

class State extends Model{
    use HasFactory, SearchableTrait,Activable,AuthorableTrait;

    protected $table = "localization_mgt__states";
    protected $guarded = [];

    protected $searchable = [
        'columns' => [
            'states.name' => 10
        ],
    ];

   // public $timestamps = false;

    public function cities(){
        return $this->hasMany(City::class,"state_id");
    }

    public function country(){
        return $this->belongsTo(Country::class,"country_id");
    }

    /** Les quartiers d'une région (à travers les villes)
     * @return HasManyThrough
     */
    public function quarters(){
        return $this->hasManyThrough(
            Quarter::class, // le model/table final (qu'on veut retourner)
            City::class, // le model/table intermédiare
            "state_id", // [FK] du model courant se trouvant dans le model intermédiare
            "city_id", // [FK] du model intermédiaire de trouvant dans le model final
            $this->getKeyName(), // [PK] du model courant
            (new City)->getKeyName() // [PK] du model intermédiare
        );
    }

    public function addresses(){
        return $this->hasMany(Address::class);
    }
}
