<?php

namespace App\Listeners;

use App\Models\Log;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class LogUserLogin
{
    /**
     * Create the event listener.
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     */
    public function handle(object $event): void
    {
        $user = $event->user;
        $userRoles = $user->roles->pluck('title')->implode(', ');

        Log::create([
            'user_type'    => $userRoles,
            'user_id'      => $user->id,
            'action'       => 'login',
            'description'  => 'User logged in',
            'ip_address'   => $user->ip_address,
        ]);
    }
}
