<?php

namespace App\Models\StockManagement;

use App\Models\LocalizationManagement\Bootables\Addressable;
use App\Models\LocalizationManagement\City;

use App\Models\StockManagement\Views\HasSales;
use App\Models\Traits\Activable;
use App\Models\Traits\Bootables\Auditable;
use Axn\EloquentAuthorable\AuthorableTrait;
use Database\Factories\WarehouseFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class Warehouse extends Model{
    use HasFactory,SoftDeletes, HasSales,
        Auditable,AuthorableTrait,Activable,Addressable,Auditable;

    protected $table = "stock_mgt__warehouses";
    protected $guarded = ['id'];
    const FK_ID = "warehouse_id";

    protected $hidden = [
        'pivot'
    ];

    protected $appends = [
      "managers_ids","cities_ids"
    ];

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

    //RELATIONS

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

    /**
     * @return BelongsTo
     */
    public function type(){
        return $this->belongsTo(WareHouseType::class,WareHouseType::FK_ID);
    }

    /**Un magasin approvisionne plusieurs points de ventes
     * @return HasMany
     */
    public function pointsOfSales(){
        return $this->hasMany(config("stocks-management.tables.warehouse"),"store_id");
    }

    /**Un point de ventes est approvionné par un magasin
     * @return BelongsTo
     */
    public function store(){
        return $this->belongsTo(config("stocks-management.tables.warehouse"),"store_id");
    }

    /**Un entrepôt a plusieurs stocks
     * @return HasMany
     */
    public function stocks(){
        return $this->hasMany(config("stocks-management.tables.stock"),"warehouse_id")
                     ->orderBy('expired_at','asc');
    }
    /**Un entrepôt a plusieurs managers
     * @return BelongsToMany
     */
    public function managers(){
        return $this->belongsToMany(
            config("stocks-management.tables.user"),
            (new WarehouseHasManager)->getTable(),
            Warehouse::FK_ID,
            "manager_id");
    }

    /** Les villes approvionnées par un entrepot
     * @return HasMany
     */
    public function cities(){
        return $this->hasMany(config("stocks-management.tables.city"),Warehouse::FK_ID);
    }

    /**Un entrepôt a plusieurs produits
     * @return BelongsToMany
     */
    public function products(){
        return $this->belongsToMany(
            config("stocks-management.tables.product"),
            (new WarehouseHasManager)->getTable(),
            Warehouse::FK_ID,
            "manager_id");
    }


    //SCOPES

    /**
     * @param $query
     * @return mixed
     */
    public function scopeStores($query){
        return $query->where(WareHouseType::FK_ID,WareHouseType::STORE);
    }

    /**
     * @param $query
     * @return mixed
     */
    public function scopepointOfSales($query){
        return $query->where(WareHouseType::FK_ID,WareHouseType::POINT_OF_SALE);
    }


    //GETTERS
    public function getManagersIdsAttribute(){
        return $this->managers->pluck('id')->toArray();
    }

    public function getCitiesIdsAttribute(){
        return $this->cities->pluck('id')->toArray();
    }
}
