<?php

namespace App\Listeners;

use App\Models\Dialler\AgentStatus;
use Illuminate\Auth\Events\Logout;

class SetAgentOfflineOnLogout
{
    public function handle(Logout $event): void
    {
        if (! $event->user) {
            return;
        }

        AgentStatus::where('agent_id', $event->user->id)
            ->where('status', '!=', 'offline')
            ->update([
                'status'            => 'offline',
                'status_changed_at' => now(),
            ]);
    }
}
