<?php

namespace App\Models\LocalizationManagement;

use App\Models\StockManagement\Traits\HasWarehouses;
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\HasOneThrough;
use Nicolaslopezj\Searchable\SearchableTrait;

class City extends Model{
    use HasFactory, SearchableTrait,Activable,HasWarehouses,AuthorableTrait;

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

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

  //  public $timestamps = false;

    /**
     * @return BelongsTo
     */
    public function state(){
        return $this->belongsTo(State::class,"city_id","state_id");
    }

    /**
     * @return HasMany
     */
    public function addresses(){
        return $this->hasMany(Address::class,"city_id");
    }

    /** Le pays (à travers la région)
     * @return HasOneThrough
     */
    public function country(){
        return $this->hasOneThrough( // [A] le model courant (TransModTown)
            Country::class, // [B] le model de retour
            State::class, // [C] le model intermédiaire en relation avec B
            (new State)->getKeyName(), // PK dans C
            (new Country)->getKeyName(), // PK dans B
            "state_id", // FK de C dans A
            'country_id' // FK de B dans C
        );
    }

    /** Les quartiers
     * @return HasMany
     */
    public function quarters(){
        return $this->hasMany(Quarter::class, "city_id");
    }
}
