<?php

namespace App\Models;

use App\Classes\General\Interfaces\Addressable;
use App\Classes\General\Interfaces\Contactable;
use App\Classes\General\Interfaces\ContainerOwner;
use App\Classes\General\Interfaces\Documentable;
use App\Classes\General\Interfaces\Packable;

use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\BusinessType;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\HasMany;

use Illuminate\Database\Eloquent\SoftDeletes;
use PhpParser\Node\Expr\AssignOp\Mod;

/**
 * Class CompanyModule
 * @package App\Models
 *
 * @property \App\Models\Company company_id 
 * @property integer type
 * @property integer status
 */
class CompanyModule extends AbstractModel implements Addressable, Documentable, Contactable, ContainerOwner, Packable
{
    use SoftDeletes;

    protected $table = 'company_modules';

    protected $dates = ['deleted_at'];

    /**
     * @return BelongsTo
     */
    public function company(): BelongsTo
    {
        return $this->BelongsTo(Company::class, 'company_id', 'id');
    }

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

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

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

    /**
     * @return belongsToMany
     */
    public function employees(): belongsToMany
    {
        return $this->belongsToMany(User::class, (new CompanyEmployee())->getTable(), 'company_module_id', 'user_id');
    }

    /**
     * @return belongsToMany
     */
    public function invites(): belongsToMany
    {
        return $this->belongsToMany(CompanyModule::class, CompanyConnection::class,'inviter_id', 'invitee_id');
    }

    /**
     * @return belongsToMany
     */
    public function inviters(): belongsToMany
    {
        return $this->belongsToMany(CompanyModule::class, CompanyConnection::class,'invitee_id', 'inviter_id');
    }


    public function connections(): hasMany {
        return $this->hasMany(CompanyConnection::class, 'invitee_id');
    }

    /**
     * @return HasMany
     */
    public function orders(): HasMany
    {
        return $this->HasMany(Order::class, 'company_module_id');
    }

     /**
      * @return belongsToMany
      */
     public function segments(): belongsToMany
     {
         return $this->belongsToMany(Segment::class, (new SegmentCompany())->getTable(), 'company_id', 'segment_id');
     }

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

     /**
      * @return HasMany
      */
     public function banks(): HasMany
     {
         return $this->HasMany(Bank::class, 'company_id');
     }

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

    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeFreightForwarders(Builder $query)
    {
        return $query->where('type', '=', BusinessType::FREIGHT_FORWARDER);
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeImporters(Builder $query)
    {
        return $query->where('type', '=', BusinessType::IMPORTER);
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeWarehouses(Builder $query)
    {
        return $query->where('type', '=', BusinessType::WAREHOUSE);
    }



}
