<?php

namespace App\Models;

use App\Classes\General\Traits\LogData;
use Illuminate\Database\Eloquent\Model;
use App\Classes\General\Interfaces\Documentable;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Staudenmeir\EloquentHasManyDeep\HasManyDeep;
use Staudenmeir\EloquentHasManyDeep\HasRelationships;

class Group extends Model implements Documentable
{
    use HasRelationships;
    use \Staudenmeir\EloquentHasManyDeep\HasTableAlias;

	public function transactions()
    {
        return $this->belongsToMany(Transaction::class, GroupTransaction::class);
    }

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

    /**
     * @return BelongsTo
     */
    public function currency(): BelongsTo
    {
        return $this->BelongsTo(Currency::class, 'currency_id', 'id');
    }


    /**
     * @return HasManyDeep
     */
    public function transferFees(): HasManyDeep
    {
        return $this->HasManyDeep(Transaction::class, [GroupTransaction::class, Transaction::class.' as alias'], ['group_id', ['owner_type', 'owner_id'], ['owner_type', 'owner_id']], ['id', null, null]);
    }

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

    /**
     * @return BelongsTo
     */
    public function original_currency(): BelongsTo
    {
        return $this->BelongsTo(Currency::class, 'original_currency_id', 'id');
    }
}
