<?php

namespace App;

use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Database\Eloquent\SoftDeletes;
use IvanSotelo\TwilioVerify\Traits\MustVerifyPhone;
use IvanSotelo\TwilioVerify\Contracts\MustVerifyPhoneContract;
use Spatie\Permission\Traits\HasRoles;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use App\Notifications\ResetPassword;
use App\Notifications\VerifyEmail;
use App\Notifications\WelcomeEmail;
use Krnos\Fire\HasOperations;
// use Controlla\ConektaCashier\Billable;
// use Controlla\ConektaCashier\Contracts\Billable as BillableContract;

class User extends Authenticatable implements JWTSubject, MustVerifyPhoneContract
{
    use Notifiable;
    use MustVerifyPhone;
    use SoftDeletes;
    use HasRoles;
    use HasOperations;
    // use Billable;

    protected $guard_name ='api';

    protected $appends = [
        'home_path', 'view_permissions', 'status',
    ];

    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = ['deleted_at', 'created_at'];

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'avatar',
    ];

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

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

    public function getViewPermissionsAttribute()
    {
        // get the names of the user's roles
        return $this->getRoleNames(); // Returns a collection
    }

    public function getHomePathAttribute()
    {
      return '/';
    }

    public function getStatusAttribute()
    {
      return $status = ($this->deleted_at) ? __('users.inactive') : __('users.active');
    }

    /**
     * Route notifications for the Nexmo channel.
     *
     * @param  \Illuminate\Notifications\Notification  $notification
     * @return string
     */
    public function routeNotificationForNexmo($notification)
    {
        return $this->phone_number;
    }

    /**
     * Route notifications for the Twilio channel.
     *
     * @param  \Illuminate\Notifications\Notification  $notification
     * @return string
     */
    public function routeNotificationForTwilio()
    {
        return $this->phone_number;
    }

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }

    /**
     * Send the password reset notification.
     *
     * @param  string  $token
     * @return void
     */
    public function sendPasswordResetNotification($token)
    {
        $this->notify(new ResetPassword($token));
    }

    /**
     * Send the email verification notification.
     *
     * @return void
     */
    public function sendEmailVerificationNotification()
    {
        $this->notify(new VerifyEmail);
    }

    /**
     * Send the email verification notification.
     *
     * @return void
     */
    public function sendEmailWelcomeNotification()
    {
        $this->notify(new WelcomeEmail);
    }
}
