<?php

namespace App\Modules\Notifications;

use Illuminate\Support\Facades\Auth;
use App\Models\ValidationModel;
use App\Models\User;

class Notification extends ValidationModel
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['user_id', 'related_type', 'related_id', 'message', 'read'];

    /**
     * Array of validation rules.
     *
     * @var  array
     */
    protected static $rules = [
        'related_id'   => 'required',
        'related_type' => 'required',
        'user_id'      => 'required',
    ];

    /**
     * Filters all notifications to display only the current user's notifications.
     * 
     * @return query
     */
    public function scopeCurrentUser($query)
    {
        return $query->where('user_id', '=', Auth::user()->id);
    }

    public function scopeUnread($query)
    {
        return $query->where('read', '=', 0);
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}
