<?php
namespace App\Model;

use App\Mail\ResetPasswordEmail;
use Cmgmyr\Messenger\Traits\Messagable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Zizaco\Entrust\Traits\EntrustUserTrait;
use Illuminate\Support\Facades\Mail;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use EntrustUserTrait { restore as private restoreA; }
    use SoftDeletes { restore as private restoreB; }
    use Notifiable;
    use Messagable;

    protected $table = "usuario";
    protected $fillable = ['nombres', 'apellido_materno', 'apellido_paterno','username','fecha_nacimiento','telefono','fecha_registro','fecha_salida','created_at','updated_at','dni','direccion','genero','estado_civil','email','password','estado','remember_token','pathimagen','urlimagen','descripcion'];
    protected $hidden = array('password');
    protected $dates = ['deleted_at'];

    public function getFullNameAttribute() {
        return ucfirst($this->nombres) . ' ' . ucfirst($this->apellido_paterno) . ' ' . ucfirst($this->apellido_materno);
    }

    public function getFullNameAttributes() {
        return ucfirst($this->apellido_paterno) . ' ' . ucfirst($this->apellido_materno) . ', ' . ucfirst($this->nombres);
    }

    public function getRoleAttribute(){
        $roles = $this->roles()->get();
        $rol = "";
        foreach($roles as $aux){
            $rol = $aux->display_name.", ".$rol;
        }
        $rol = rtrim($rol, ', ');
        return $rol;
    }

    public function getRoleIdAttribute(){
        return $this->roles()->first()->id;
    }

    public function roles(){
        return $this->belongsToMany('App\Model\Role');
    }

    public function isRole($roleId)
    {
        foreach ($this->roles()->get() as $role)
        {
            if ($role->id == $roleId)
            {
                return true;
            }
        }
        return false;
    }

    public function isAdmin()
    {
        $roleId = config('constants.ROLES.ADMINISTRADOR');
        foreach ($this->roles()->get() as $role)
        {
            if ($role->id == $roleId)
            {
                return true;
            }
        }
        return false;
    }

    public function experiencias() {
        return $this->hasMany('App\Model\ExperienciaLaboral', 'id_usuario')
            ->select('id','fecha_inicio','fecha_fin','trabajo_actual','nombre_empresa','cargo','tareas_desarrolladas');
    }

    public function sendPasswordResetNotification($token)
    {
        Mail::to($this->email)->send(new ResetPasswordEmail(new ResetPassword("Haz recibido este correo electrónico porque recibimos una solicitud de restablecimiento de contraseña para tu cuenta.",$token,"Si no solicitó un restablecimiento de contraseña, no es necesario realizar alguna acción.","
If you’re having trouble clicking the \"Recuperar contraseña\" button, copy and paste the URL below into your web browser"), $this));
    }

    public function restore()
    {
        $this->restoreA();
        $this->restoreB();
    }
}
