@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 && $lastMessage->is_read == false && $lastMessageSender != 'Tú')