<?php

// 4. Trait for User Models (add to all your auth models)
namespace App\Traits;

use App\Services\PresenceService;

trait HasPresence
{
    public function updatePresence($guard = null)
    {
        app(PresenceService::class)->updatePresence($this, $guard);
    }

    public function isOnline($guard = 'web', $minutes = 5)
    {
        return app(PresenceService::class)->isUserOnline($this, $guard, $minutes);
    }

    public function getLastSeenAttribute($guard = 'web')
    {
        return app(PresenceService::class)->getUserLastSeen($this, $guard);
    }

    public function scopeOnline($query, $guard = 'web', $minutes = 5)
    {
        $onlineUsers = app(PresenceService::class)->getOnlineUsers($guard, $minutes);
        $onlineIds = $onlineUsers->where('user_type', static::class)->pluck('id');

        return $query->whereIn('id', $onlineIds);
    }
}