<?php

namespace App\Laravue\Models;

use App\Counseling;
use App\UserConversation;
use App\UserProject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
use Laravel\Sanctum\HasApiTokens;

/**
 * Class User
 *
 * @property string $name
 * @property string $email
 * @property string $password
 * @property Role[] $roles
 *
 * @method static User create(array $user)
 * @package App
 */
class User extends Authenticatable
{
	use Notifiable, HasRoles, HasApiTokens;
	
	/**
	 * The attributes that are mass assignable.
	 *
	 * @var array
	 */
	protected $fillable = [
		'name', 'slug', 'first_name', 'last_name', 'full_name', 'furigana_name', 'furigana_first_name', 'furigana_last_name',
		'username', 'email', 'email2', 'password', 'avatar', 'gender', 'career', 'date_of_birth', 'age', 'post_code', 'city', 'district', 'address', 'address2',
		'phone_number', 'fax_number', 'mobile_tel', 'emg_tel', 'introduction', 'reminder_question', 'reminder_answer', 'line_id', 'point', 'point_rate',
		'driver_license', 'insurance_card', 'number_card', 'shipping_post_code', 'shipping_city', 'shipping_district', 'shipping_address', 'shipping_address2',
		'is_leader', 'is_allowed_record_single', 'is_allowed_report_single', 'is_allowed_record_group', 'is_allowed_report_group', 'time_allowed_record',
		'reset_password_code', 'reset_password_time', 'email_verified_at', 'email_subscribe', 'email_format', 'login_url', 'is_online', 'last_activity',
		'fcm_token', 'push_notifications_enabled', 'pwa_endpoint', 'pwa_p256dh', 'pwa_auth', 'pwa_user_agent', 'pwa_platform',
		'is_temp', 'is_activated', 'is_deleted', 'created_by', 'created_at', 'updated_at',
	];
	
	/**
	 * The attributes that should be hidden for arrays.
	 *
	 * @var array
	 */
	protected $hidden = [
		'password', 'remember_token',
	];
	
	/**
	 * The attributes that should be cast to native types.
	 *
	 * @var array
	 */
	protected $casts = [
		'email_verified_at' => 'datetime',
		'is_online'         => 'boolean',
		'last_activity'     => 'datetime'
	];
	
	/**
	 * Set permissions guard to API by default
	 * @var string
	 */
	protected $guard_name = 'api';
	
	/**
	 * @inheritdoc
	 */
	public function getJWTIdentifier()
	{
		return $this->getKey();
	}
	
	/**
	 * @inheritdoc
	 */
	public function getJWTCustomClaims()
	{
		return [];
	}
	
	/**
	 * Check if user has a permission
	 * @param String
	 * @return bool
	 */
	public function hasPermission($permission): bool
	{
		foreach ($this->roles as $role) {
			if (in_array($permission, $role->permissions->toArray())) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * @return bool
	 */
	public function isAdmin(): bool
	{
		foreach ($this->roles as $role) {
			if ($role->isAdmin()) {
				return true;
			}
		}
		
		return false;
	}
	
	public function projects()
	{
		return $this->hasMany(UserProject::class, 'user_id', 'id')->with('project', 'members');
	}
	
	public function conversations()
	{
		return $this->hasMany(UserConversation::class, 'user_id', 'id');
	}
}
