@foreach($chats as $chat)
@php
// Determine the correct recipient name
$recipient = ($chat->user_id === auth()->id()) ? $chat->recipient : $chat->user;
$lastMessage = $chat->messages->first(); // Get the last message (already fetched in the query)
// Determine if the last message is from the logged-in user or the recipient
$lastMessageSender = $lastMessage && $lastMessage->user_id === auth()->id() ? 'TĂș' : $recipient->name;
@endphp
@if($lastMessage)