<?php

namespace App\Models;

use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Foundation\Auth\User as AuthenticatableUser;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Support\Facades\Hash;
use Illuminate\Notifications\Notifiable;
use App\Notifications\ResetAppUserPassword;




class User extends AuthenticatableUser implements AuthenticatableContract, AuthorizableContract, JWTSubject
{
    use Authenticatable, Authorizable, Notifiable;

    const STATE_ACTIVE = 'active';
    const STATE_INACTIVE = 'inactive';

    // duration, in minutes, that a delete request is valid
    const DELETE_USER_REQUEST_EXPIRATION = 60;

    protected $dates = [
        'created_at',
        'updated_at',
        'last_login',
        'recover_date',
        'delete_request_date'
    ];

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

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'recover_token', 'delete_request_token'
    ];


    protected static function boot()
    {
        // sets default language to 'pt' when saving the model
        static::saving(function ($model) {
            $model->language_id = $model->language_id ?? config('api.default_user_lang');
        });
        parent::boot();
    }


    public function devices()
    {
        return $this->hasMany(Device::class);
    }

    public function city()
    {
        return $this->belongsTo(City::class);
    }


    /**
     * Get all of the favorite artists for the user.
     */
    public function favouriteArtists()
    {
        return $this->morphedByMany(Artist::class, 'favourite');
    }

    /**
     * Get all of the favorite collections for the user.
     */
    public function favouriteCollections()
    {
        return $this->morphedByMany(Collection::class, 'favourite');
    }

    /**
     * Get all of the favorite exhibitions for the user.
     */
    public function favouriteExhibitions()
    {
        return $this->morphedByMany(Exhibition::class, 'favourite');
    }


    /**
     * Get all the fcm subscription topics the user has.
     */
    public function fcmSubscriptions()
    {
        return $this->hasMany(UserFcmSubscription::class);
    }

    /**
     * Get all the fcm subscription topics the user has.
     */
    public function fcmSubscriptionTopics()
    {
        return $this->hasMany(UserFcmSubscription::class)->select(['topic']);
    }



    /**
     * 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 [];
    }

    /**
     * Set the user's password as encrypted.
     *
     * @param  string $value
     * @return void
     */
    public function setPasswordAttribute($value)
    {
        if (is_null($value)) {
            $this->attributes['password'] = null;
        } else {
            $this->attributes['password'] = Hash::make($value);
        }
    }

    /**
     * Send a password reset notification to the user.
     *
     * @param  string  $token
     * @return void
     */
    public function sendPasswordResetNotification($token)
    {

        $this->notify(new ResetAppUserPassword($token));
    }
}
