<?php

namespace App\Models;

use App\Classes\General\Interfaces\Remarkable;
use App\Classes\General\Interfaces\Transportable;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Staudenmeir\EloquentHasManyDeep\HasManyDeep;
use Staudenmeir\EloquentHasManyDeep\HasRelationships;

class Container extends AbstractModel implements Transportable, Remarkable
{
    use HasRelationships;
    use SoftDeletes;

    protected $table = 'containers';

    protected $fillable = ['status'];

    protected $dates = ['loading_date'];

    public function packingLists(): belongsToMany
    {
        return $this->belongsToMany(PackingList::class, ContainerPackingList::class, 'container_id', 'packing_list_id');
    }

    /**
     * @return hasManyDeep
     */
    public function packages(): hasManyDeep
    {
        return $this->hasManyDeepFromRelations($this->packingLists(), (new PackingList())->packages());
    }

    /**
     * @return hasManyDeep
     */
    public function companyModules(): hasManyDeep
    {
        return $this->hasManyDeep(CompanyModule::class, [ContainerPackingList::class, PackingList::class, Order::class], ['container_id', 'id', 'id', 'id'], ['id', 'packing_list_id', 'owner_id', 'company_module_id']);
    }

    /**
     * @return hasManyDeep
     */
    public function orders(): hasManyDeep
    {
        return $this->hasManyDeep(Order::class, [ContainerPackingList::class, PackingList::class], ['container_id', 'id', 'id'], ['id', 'packing_list_id', 'owner_id']);
    }

    /**
     * @return MorphMany
     */
    public function transports(): morphMany
    {
        return $this->morphMany(Transport::class, 'owner');
    }

    /**
     * @return MorphMany
     */
    public function remarks(): morphMany
    {
        return $this->morphMany(Remark::class, 'owner');
    }
}
