<?php

namespace App\Models;

use App\Classes\General\Interfaces\Steppable;
use App\Classes\General\Interfaces\Transportable;
use App\Classes\General\Interfaces\Packable;
use App\Classes\General\Interfaces\Transactionable;

use App\Classes\General\Traits\LogData;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Staudenmeir\EloquentHasManyDeep\HasManyDeep;
use Staudenmeir\EloquentHasManyDeep\HasRelationships;
use Staudenmeir\EloquentHasManyDeep\HasTableAlias;

class PackingList extends AbstractModel implements Transportable, Steppable, Packable, Transactionable
{
    use HasTableAlias;
    use HasRelationships;
    use SoftDeletes;
    use LogData;

    protected $table = 'packing_lists';

    protected $fillable = ['status'];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
     */
    public function owner(): morphTo
    {
        return $this->morphTo();
    }

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

    public function containers(): BelongsToMany
    {
        return $this->BelongsToMany(Container::class, ContainerPackingList::class);
    }

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

    /**
     * @return HasMany
     */
    public function packages(): hasMany
    {
        return $this->hasMany(Package::class, 'packing_list_id');
    }

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

    /**
     * @return HasManyDeep
     */
    public function replicatedPackages(): HasManyDeep
    {
//        return $this->hasManyDeep(Package::class, [PackingList::class.' as replica', PackingList::class], [['owner_type', 'owner_id'], ['owner_type', 'owner_id'], 'packing_list_id'], [null, null, 'id']);
        return $this->hasManyDeepFromRelations($this->packingLists(), (new PackingList)->setAlias('replica')->packages());
    }

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

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

    /**
     * @return belongsToMany
     */
    public function packing_list_owner()
    {
        return $this->BelongsToMany(PackingList::class, 'packing_list_owner', 'owner_packing_list_id', 'packing_list_id');
    }

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

    /**
     * @return hasManyDeep
     */
    public function shippingTransports(): hasManyDeep
    {
        return $this->hasManyDeep(Transport::class, [ContainerPackingList::class, Container::class], ['packing_list_id', 'id', ['owner_type', 'owner_id']], ['id', 'container_id', null]);
    }

    /**
     * @return hasManyDeep
     */
    public function shippingSchedules(): hasManyDeep
    {
        return $this->hasManyDeep(Schedule::class, [ContainerPackingList::class, Container::class, Transport::class], ['packing_list_id', 'id', ['owner_type', 'owner_id'], ['owner_type', 'owner_id']], ['id', 'container_id', null, null]);
    }


    /**
     * @return hasManyDeep
     */
    public function deliverySchedules(): hasManyDeep
    {
        return $this->hasManyDeep(Schedule::class, [Transport::class], [['owner_type', 'owner_id'], ['owner_type', 'owner_id']], [null, null]);
    }

    /**
     * @param $query
     * @return mixed
     */
    public function scopeShipping($query){
        return $query->whereHas('shippingSchedules', function($schedule){
            return $schedule->where('etd', '<', Carbon::now())->where('schedules.status', ApprovalStatus::APPROVED);
        })->whereDoesntHave('shippingTransports', function($transport){
            return $transport->where('transport_arrangements.status', ApprovalStatus::COMPLETED);
        });
    }

}
