<?php

namespace App\Models;

use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\PackingListType;
use App\Classes\ValueObjects\Constants\RoleTypes;
use App\Scopes\CustomerOrdersScope;
use App\Scopes\CustomerPackagesScope;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
use Illuminate\Database\Eloquent\SoftDeletes;
use Staudenmeir\EloquentHasManyDeep\HasManyDeep;
use Staudenmeir\EloquentHasManyDeep\HasOneDeep;
use Staudenmeir\EloquentHasManyDeep\HasRelationships;

class Package extends AbstractModel
{
    use HasRelationships;
    use SoftDeletes;

    protected $table = 'packages';

    /**
     * @return BelongsTo
     */
    public function packingList(): BelongsTo
    {
        return $this->belongsTo(PackingList::class, 'packing_list_id');
    }

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

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

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

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


    /**
     * @return HasManyThrough
     */
    public function deliveryTransports(): hasManyThrough
    {
        return $this->hasManyDeep(Transport::class, [PackingList::class], ['id', ['owner_type', 'owner_id']], ['packing_list_id', null]);
    }

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

    /**
     * @return hasOneDeep
     */
    public function container(): hasOneDeep
    {
        return $this->hasOneDeep(Container::class, [PackingList::class, ContainerPackingList::class], ['id', 'packing_list_id', 'id'], ['packing_list_id', 'id', 'container_id']);
    }

    /**
     * @return hasOneDeep
     */
    public function transport(): hasOneDeep
    {
        return $this->hasOneDeep(Transport::class, [PackingList::class], ['id', ['owner_type', 'owner_id']], ['packing_list_id', 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);
        });
    }

    /**
     * @param $query
     * @return mixed
     */
    public function scopeShipped($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);
        });
    }

    /**
     * @param $query
     * @return mixed
     */
    public function scopeShippingList($query){
        return $query->whereHas('packingList', function($packingList){
            return $packingList->where('type', PackingListType::SHIPPING_PACKING_LIST);
        });
    }

    /**
     * @param $query
     * @return mixed
     */
    public function scopeWarehouseReceiveList($query){
        return $query->whereHas('packingList', function($packingList){
            return $packingList->where('type', PackingListType::WAREHOUSE_RECEIVE_LIST);
        });
    }

    protected static function booted()
    {
        if (auth()->user()->type === RoleTypes::USER) {
//            static::addGlobalScope(new CustomerPackagesScope());
        }
    }

}
