<x-app-layout>
    <x-slot name="header">
        <div class="flex flex-col sm:flex-row justify-between items-start sm:items-center gap-3 sm:gap-4">
            <div class="min-w-0">
                <p class="text-xs font-black tracking-widest text-[#15db00] uppercase">Chat Dukungan</p>
                <h2 class="mt-1 font-black text-xl sm:text-2xl text-slate-900 tracking-tight leading-tight truncate">
                    {{ __('Percakapan dengan :nama', ['nama' => $contact->nama]) }}
                </h2>
            </div>
            <a href="{{ route('support.index') }}" class="inline-flex items-center gap-2 px-4 py-2 sm:px-5 sm:py-2.5 bg-slate-900 hover:bg-slate-700 text-xs font-bold text-white rounded-xl shadow-sm transition-all duration-200 shrink-0">
                <svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2.5" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" d="M6.75 15.75L3 12m0 0l3.75-3.75M3 12h18" />
                </svg>
                <span>Kembali</span>
            </a>
        </div>
    </x-slot>

    <div class="py-4 sm:py-6 bg-slate-100/70 min-h-screen">
        <div class="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8">

            @if (session('success'))
                <div class="mb-3 sm:mb-4 bg-emerald-50 border border-emerald-200 text-emerald-800 px-4 py-3 rounded-xl flex items-center gap-3 shadow-sm" role="alert">
                    <svg class="w-5 h-5 text-emerald-500 shrink-0" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
                    </svg>
                    <span class="text-sm font-medium">{{ session('success') }}</span>
                </div>
            @endif

            @if ($errors->any())
                <div class="mb-3 sm:mb-4 bg-rose-50 border border-rose-200 text-rose-700 px-4 py-3 rounded-xl text-sm font-medium">
                    <ul class="list-disc list-inside space-y-1">
                        @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif

            <!-- Info Pengguna -->
            <div class="bg-white rounded-2xl shadow-sm border border-slate-200 p-3 sm:p-4 mb-3 sm:mb-4 flex items-center justify-between gap-3">
                <div class="flex items-center gap-3 min-w-0">
                    <span class="w-9 h-9 sm:w-10 sm:h-10 rounded-2xl bg-cashev-blue/10 text-cashev-blue flex items-center justify-center text-base sm:text-lg shrink-0">👤</span>
                    <div class="min-w-0">
                        <p class="font-black text-slate-900 text-sm truncate">{{ $contact->nama }}</p>
                        <p class="text-xs text-slate-500 truncate">WA: {{ $contact->whatsapp }} · {{ $contact->usaha ?: '-' }}</p>
                    </div>
                </div>
                @if ($contact->status !== 'closed')
                    <form method="POST" action="{{ route('support.close', $contact->id) }}" onsubmit="return confirm('Tutup percakapan ini?')" class="shrink-0">
                        @csrf
                        <button type="submit" class="text-xs font-bold text-slate-500 bg-slate-100 hover:bg-slate-200 px-3 py-1.5 rounded-lg transition-colors">
                            Tutup Chat
                        </button>
                    </form>
                @else
                    <span class="text-xs font-bold text-slate-400 bg-slate-100 px-3 py-1.5 rounded-lg shrink-0">Selesai</span>
                @endif
            </div>

            <!-- Area Chat -->
            <div class="bg-white rounded-2xl shadow-sm border border-slate-200 overflow-hidden flex flex-col" style="height: calc(100vh - 280px); min-height: 400px;">
                
                <!-- Messages -->
                <div class="flex-1 overflow-y-auto p-3 sm:p-4 md:p-6 space-y-3 sm:space-y-4" id="chat-messages">
                    @forelse ($contact->messages as $msg)
                        <div class="flex {{ $msg->sender === 'admin' ? 'justify-end' : 'justify-start' }}">
                            <div class="max-w-[80%] sm:max-w-[75%] md:max-w-[60%]">
                                <div class="flex items-end gap-2 {{ $msg->sender === 'admin' ? 'flex-row-reverse' : 'flex-row' }}">
                                    @if ($msg->sender === 'user')
                                        <span class="w-6 h-6 sm:w-7 sm:h-7 rounded-xl bg-slate-200 text-slate-600 flex items-center justify-center text-[10px] font-black shrink-0">U</span>
                                    @else
                                        <span class="w-6 h-6 sm:w-7 sm:h-7 rounded-xl bg-cashev-blue text-white flex items-center justify-center text-[10px] font-black shrink-0">A</span>
                                    @endif
                                    <div>
                                        <div class="px-3 py-2 sm:px-4 sm:py-2.5 rounded-2xl text-sm leading-relaxed {{ $msg->sender === 'admin' ? 'bg-cashev-blue text-white rounded-br-md' : 'bg-slate-100 text-slate-800 rounded-bl-md' }}">
                                            {!! nl2br(e($msg->message)) !!}
                                        </div>
                                        <p class="text-[10px] text-slate-400 mt-1 px-1 {{ $msg->sender === 'admin' ? 'text-right' : 'text-left' }}">
                                            {{ $msg->created_at?->format('H:i') }}
                                            @if ($msg->sender === 'user' && $msg->read_at)
                                                <span class="ml-1 text-cashev-blue">✓✓</span>
                                            @endif
                                        </p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    @empty
                        <div class="flex flex-col items-center justify-center h-full text-center gap-3 px-4">
                            <span class="w-14 h-14 sm:w-16 sm:h-16 rounded-3xl bg-slate-100 text-slate-400 flex items-center justify-center text-2xl sm:text-3xl">📭</span>
                            <div>
                                <p class="text-sm font-bold text-slate-700">Belum ada pesan</p>
                                <p class="text-xs text-slate-500 mt-1">Tunggu pengguna mengirim pesan pertama.</p>
                            </div>
                        </div>
                    @endforelse
                    <div id="chat-bottom"></div>
                </div>

                <!-- Input Area -->
                @if ($contact->status !== 'closed')
                    <div class="border-t border-slate-100 px-3 py-3 sm:px-4 sm:py-4 bg-slate-50">
                        <form method="POST" action="{{ route('support.reply', $contact->id) }}" class="flex gap-2 sm:gap-3 items-end">
                            @csrf
                            <div class="flex-1">
                                <textarea
                                    name="admin_reply"
                                    rows="1"
                                    required
                                    placeholder="Ketik balasan admin..."
                                    class="w-full px-3 py-2.5 sm:px-4 sm:py-3 rounded-2xl border border-slate-200 bg-white text-slate-900 text-sm outline-none focus:ring-2 focus:ring-[#15db00] resize-none"
                                    style="min-height: 44px; max-height: 120px;"
                                    onkeydown="if(event.key==='Enter'&&!event.shiftKey){event.preventDefault();this.form.submit()}"
                                ></textarea>
                            </div>
                            <button type="submit" class="w-10 h-10 sm:w-11 sm:h-11 rounded-2xl bg-[#15db00] hover:bg-[#12c400] text-white flex items-center justify-center transition shadow-md shadow-[#15db00]/20 shrink-0">
                                <svg class="w-5 h-5" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" d="M6 12L3.269 3.126A59.768 59.768 0 0121.485 12 59.77 59.77 0 013.27 20.876L5.999 12zm0 0h7.5" />
                                </svg>
                            </button>
                        </form>
                        <p class="text-[10px] text-slate-400 mt-2 text-center">Enter untuk mengirim · Shift+Enter untuk baris baru</p>
                    </div>
                @else
                    <div class="border-t border-slate-100 px-4 py-4 bg-slate-50 text-center">
                        <p class="text-xs font-bold text-slate-400">Percakapan ini sudah ditutup.</p>
                    </div>
                @endif
            </div>

        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const chatBox = document.getElementById('chat-messages');
            if (chatBox) chatBox.scrollTop = chatBox.scrollHeight;
        });
    </script>
</x-app-layout>
