<?php


namespace App\Models\LocalizationManagement;


use App\Models\Traits\Activable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
use Illuminate\Database\Eloquent\SoftDeletes;
use Staudenmeir\EloquentHasManyDeep\HasOneDeep;
use Staudenmeir\EloquentHasManyDeep\HasRelationships;

class Quarter extends Model {

    use HasFactory,SoftDeletes,Activable,
        HasRelationships;


    protected $table = "localization_mgt__quarters";
    protected $guarded = ["created_at"];
    const FK_ID = "quarter_id";

    public function getRouteKeyName(){
        return "id";
    }


    protected static function newFactory(){
        //return QuarterFactory::new();
    }


    //RELATIONS
    public function city(){
        return $this->belongsTo(City::class,"city_id");
    }

    /**
     * @return HasOneThrough
     */
    public function region(){
        return $this->hasOneThrough(
            State::class,
            City::class,
            "state_id",
            (new State)->getKeyName(),
            (new City)->getKeyName(),
            (new State)->getKeyName()
        );
    }

    /** Le pays (à travers la ville et la région)
     * @return HasOneDeep
     */
    public function country(){
        return $this->hasOneDeep( // Model courant (TransModQuarter) [A]
            Country::class, // [B] Model de retour
            [
                City::class, // [C] 1er Model intermédiaire
                State::class, // [D] 2e Model intermédiaire
            ],
            [
                "city_id", // [FK1] de C dans A
                "state_id", // [FK2] de D dans C
                "country_id", // [FK3] de B dans D
            ],
            [
                $this->getKeyName(), // [PK1] de A
                (new State)->getKeyName(), // [PK2] de D
                (new Country)->getKeyName(), // [PK3] de B
            ]
        );

        /**
         * D.PK3 = B.FK3
         * C.PK2 = D.FK2
         * A.PK1 = C.FK1
         * A.FK1 = ?
         */
    }

    /** L'état (à travers la ville)
     * @return HasOneThrough
     */
    public function state(){
        return $this->hasOneThrough( // [A] le model courant (TransModQuarter)
            State::class, // [B] le model de retour
            City::class, // [C] le model intermédiaire en relation avec B
            (new City)->getKeyName(), // PK dans C
            (new State)->getKeyName(), // PK dans B
            "city_id", // FK de C dans A
            'state_id' // FK de B dans C
        );
    }
}
