<?php

namespace App\Modules\Bookings;

use App\Models\ValidationModel;
use App\Modules\Bookings\Task;
use App\Modules\Clients\Client;
use App\Modules\Clients\Customer;

class MobileToken extends ValidationModel
{
//    use PresentableTrait;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['token', 'related_id','related_type'];

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'mobile_tokens';

    /**
     * Array of validation rules.
     *
     * @var  array
     */
    protected static $rules = [
        'token'   => 'required',
        'related_id'   => 'required',
        'related_type' => 'required',
    ];

    /**
     * The "booting" method of the model.
     *
     * @return void
     */
    public static function boot()
    {
        parent::boot();

    }

    /**
     * Returns the related model.
     *
     * @return App\Modules\Bookings\Task or App\Modules\Clients\Customer depending on type.
     */
     public function related()
     {
       if ($this->related_type == 'task')
       {
         return $this->belongsTo(Task::class, 'related_id', 'id');
       }
       else if ($this->related_type == 'client')
       {
         return $this->belongsTo(Client::class, 'related_id', 'id');
       }
       else
       {
         return $this->belongsTo(Customer::class, 'related_id', 'id');
       }
     }

    public function task()
    {
        return $this->belongsTo(Task::class, 'related_id', 'id');
    }

    public function client()
    {
        return $this->belongsTo(Client::class, 'related_id', 'id');
    }

    public function customer()
    {
        return $this->belongsTo(Customer::class, 'related_id', 'id');
    }

    /**
     * Filters comments to unread messages.
     *
     * @return query
     */
    public function scopeUnread($query)
    {
        return $query->where('read', '=', 0);
    }

    /**
     * Filters comments to read messages.
     *
     * @return query
     */
    public function scopeRead($query)
    {
        return $query->where('read', '=', 1);
    }

    public function getTokens($type,$id){

        echo "fetching da tokens\n";
        echo $this->related_type . "\n";

        $tokens = $this->related()
                    ->where('related_type','=',$type)
                    ->where('related_id','=',$id)
                    ->get();

        return $tokens;
    }
}
