@foreach ($ticket->chats as $chat)
@php
$isMe = $chat->sender_id == $authUser->id;
$showSeenTime = $isMe && (int) $chat->id === (int) $latestSeenMessageId && $chat->seen_at;
$senderRoleLabel = '';
if ($isBookingTicket && !$isMe) {
if ((int) $chat->sender_id === (int) $ticket->user_id) {
$senderRoleLabel = ' (Customer)';
} elseif ($ticket->assigned_to && (int) $chat->sender_id === (int) $ticket->assigned_to) {
$senderRoleLabel = ' (Partner)';
}
}
@endphp
@if(!$isMe)
{{ $avatarInitial($chat->sender->name ?? '') }}
@endif
{{ ($chat->sender->name ?? 'User') . $senderRoleLabel }}{{ $chat->created_at ? ', ' . $chat->created_at->format('d M, h:i A') : '' }}
@php
$trimmedMessage = trim((string) ($chat->message ?? ''));
@endphp
@if($trimmedMessage !== '')
{{ $trimmedMessage }}
@endif
@if($chat->attachment)
@php
$attachmentUrl = $chat->attachment_url ?? (\Illuminate\Support\Str::startsWith($chat->attachment, ['http://', 'https://'])
? $chat->attachment
: asset(\Illuminate\Support\Str::startsWith($chat->attachment, 'storage/') ? $chat->attachment : 'storage/' . ltrim($chat->attachment, '/')));
$fileName = basename(parse_url($chat->attachment, PHP_URL_PATH) ?? $chat->attachment);
$ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
$isImage = $chat->message_type === 'image' || in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg'], true);
@endphp
@if($isImage)
@else
@endif
@endif
@if($isMe)
{{ $showSeenTime ? 'Seen: ' . $chat->seen_at->format('d M, h:i A') : '' }}
@endif
@endforeach