<?php

namespace App;

use App\Module;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\SoftDeletes;


class User extends Authenticatable
{
    use Notifiable;
    use SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password' ,'status', 'phone', 'photo','role','age','country_id','relative','category_id','type_id','gender','active','code','language','device_token','bank_acc','iban','bank','cost','wallet'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token','api_token'
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    /**
     * Get the modules of this user
     * @return BelongsToMany
     */
    public function modules()
    {
        return $this->belongsToMany(Module::class,'user_modules');
    }
    public function type(){
         return $this->belongsTo(Type::class,'type_id');
    }
    public function country(){
         return $this->belongsTo(Country::class,'country_id');
    }
    public function category(){
         return $this->belongsTo(Category::class,'category_id');
    }
    
    public function experience(){
         return $this->hasMany(Experience::class);
    }
    public function notifications(){
         return $this->hasMany(Notification::class);
    }
    public function currents(){
         return $this->hasMany(Channel::class)->where('user_id',\Auth::user()->id)->where('status','!=','finished');
    }
    public function pervious(){
         return $this->hasMany(Channel::class)->where('user_id',\Auth::user()->id)->where('status','finished');
    }
    
    

    public function certification(){
         return $this->hasMany(Certification::class);
    }
    public function rates(){
         return $this->hasMany(Rate::class);
    }
    
    /**
     * Check if the user can access a module
     * @param string, module name
     * @return bool
     */
    public function hasModule($module)
    {
        if($this->modules->contains('path',$module))
        {
            return true;
        }
        return false;
    }

    /**
     * Check if the user can access a module
     * @param string, module name
     * @return bool
     */
    public function canAccess($module)
    {
        // dd($this->role);
        if ( 'admin' == $this->role ) {
            return true;
        }else {
            return $this->hasModule($module);
        }
        return false;
    }
}
