<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * Notification received by a user
 */
class Notification extends Model
{
    /** Recipient user */
    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    /** User who triggered this notification */
    public function actor()
    {
        return $this->belongsTo(User::class, 'actor_id');
    }

    /** Notification context (what was affected) */
    public function context()
    {
        return $this->morphTo();
    }

    /**
     * Unseen notifications
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeUnseen($query)
    {
        return $query->where('seen', false);
    }

    /**
     * Seen notifications
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeSeen($query)
    {
        return $query->where('seen', true);
    }
}