<?php

namespace App\Listeners;

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

class LogLogout
{
    public function handle(Logout $event): void
    {
        // 🛑 HANDLE CASE: user sudah null (session expired / forced logout)
        if (! $event->user) {
            activity('auth')
                ->withProperties([
                    'ip' => request()->ip(),
                    'agent' => request()->userAgent(),
                ])
                ->log('logout - user session expired / forced logout');

            return; // <-- exit, because there is no user to inject
        }

        // 🧍 user ada → pastikan pakai Model User asli
        $user = $event->user instanceof User
            ? $event->user
            : User::find($event->user->id);

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