<?php

namespace App\Listeners;

use App\Models\User;
use Illuminate\Auth\Events\Login;

class LogSuccessfulLogin
{
    public function handle(Login $event): void
    {
        $user = $event->user instanceof User
            ? $event->user
            : User::find($event->user->id);

        if (! get_setting('login_log_enabled', true)) {
            return;
        }

        activity('auth')
            ->causedBy($user)
            ->withProperties([
                'email' => $user?->email,
                'ip' => request()->ip(),
                'agent' => request()->userAgent(),
            ])
            ->log('login');
    }
}
