@extends('layouts.admin')

@section('title', 'Chat dengan ' . $lawan->nama_lengkap)

@section('content')
    <section class="section">
        <div class="card" style="height: 85vh; display: flex; flex-direction: column;">

            {{-- HEADER CHAT --}}
            <div class="card-header bg-primary py-3 d-flex align-items-center justify-content-between">
                <div class="d-flex align-items-center text-white">
                    <a href="{{ route('petani.pesan.index') }}" class="btn btn-light btn-sm me-3 rounded-circle"
                        style="width: 32px; height: 32px; padding: 0; display: flex; align-items: center; justify-content: center;">
                        <i class="bi bi-arrow-left text-primary"></i>
                    </a>
                    <div>
                        <h6 class="mb-0 text-white font-bold" style="font-size: 1.1rem;">{{ $lawan->nama_lengkap }}</h6>
                        <small style="opacity: 0.8; font-size: 0.8rem;">
                            {{ $lawan->role ?? 'Pembeli' }}
                        </small>
                    </div>
                </div>
            </div>

            {{-- INBOX SECTION (SCROLLABLE) --}}
            <div class="card-body p-4" id="chatContainer"
                style="flex-grow: 1; overflow-y: auto; background-color: #f2f7ff;">
                <div class="d-flex flex-column gap-3">
                    @forelse($chats as $chat)
                        @php
                            $isMe =
                                $chat->pengirim_id == Auth::guard('petani')->id() &&
                                $chat->pengirim_type == 'App\Models\Petani';
                        @endphp

                        {{-- Logic Posisi: Kalau SAYA di Kanan (end), Kalau DIA di Kiri (start) --}}
                        <div class="d-flex w-100 {{ $isMe ? 'justify-content-end' : 'justify-content-start' }}">

                            <div style="max-width: 70%; min-width: 120px;">
                                {{-- Bubble Chat --}}
                                <div class="p-3 shadow-sm position-relative"
                                    style="border-radius: 15px; 
                                        border-{{ $isMe ? 'bottom-right' : 'bottom-left' }}-radius: 0;
                                        background-color: {{ $isMe ? '#435ebe' : '#ffffff' }}; 
                                        color: {{ $isMe ? '#ffffff' : '#212529' }};">

                                    <p class="mb-1" style="font-size: 0.95rem; line-height: 1.4;">
                                        {{ $chat->isi_pesan }}
                                    </p>

                                    <div class="d-flex justify-content-end align-items-center mt-1">
                                        <small style="font-size: 0.7rem; opacity: 0.8; margin-right: 4px;">
                                            {{ $chat->created_at->format('H:i') }}
                                        </small>
                                        @if ($isMe)
                                            <i class="bi {{ $chat->sudah_dibaca ? 'bi-check-all text-info' : 'bi-check' }}"
                                                style="font-size: 0.9rem;"></i>
                                        @endif
                                    </div>
                                </div>
                            </div>
                        </div>
                    @empty
                        <div class="text-center my-5">
                            <span class="badge bg-light-secondary text-secondary p-3 rounded-pill">
                                Belum ada percakapan. Mulailah menyapa! 👋
                            </span>
                        </div>
                    @endforelse
                </div>
            </div>

            {{-- FOOTER INPUT PESAN --}}
            <div class="card-footer bg-white py-3 border-top">
                <form action="{{ route('pesan.kirim') }}" method="POST" class="d-flex gap-2 align-items-center">
                    @csrf
                    {{-- Input Hidden Data Penerima --}}
                    <input type="hidden" name="penerima_id" value="{{ $lawan->id }}">

                    {{-- Input Text --}}
                    <div class="input-group">
                        <input type="text" name="isi_pesan" class="form-control form-control-lg border-0 bg-light"
                            placeholder="Ketik pesan..." required autocomplete="off" style="border-radius: 20px;">

                        <button type="submit" class="btn btn-primary btn-lg ms-2 rounded-circle"
                            style="width: 50px; height: 50px; display: flex; align-items: center; justify-content: center;">
                            <i class="bi bi-send-fill fs-5"></i>
                        </button>
                    </div>
                </form>
            </div>

        </div>
    </section>

    <script>
        document.addEventListener("DOMContentLoaded", function() {
            var chatBox = document.getElementById("chatContainer");
            if (chatBox) {
                chatBox.scrollTop = chatBox.scrollHeight;
            }
        });
    </script>
@endsection
