{{--
    Live Chat Widget — login client (mode=client) + misafir (mode=guest, PRC-606).
    chat_enabled açıkken render olur; misafir için ayrıca chat_guest_enabled gerekir.

    Backend: store.chat.* public route'ları (/chat/start, /chat/{session}/{send,messages,close}).
    Real-time: Laravel Echo + Reverb — client private `chat.{id}`, misafir public `chat.guest.{token}`.
    Misafir önce ad+e-posta ön-formunu doldurur; bağlantı koparsa manuel "yenile".
--}}
@php
    $chatEnabled = (bool) setting('chat_enabled', false);
    $chatGuestEnabled = (bool) setting('chat_guest_enabled', false);
    $chatIsClient = auth('client')->check();
    $chatMode = $chatIsClient ? 'client' : 'guest';
    $chatBlocked = ! $chatIsClient && \App\Models\ChatBlock::isBlocked(\App\Helpers\VisitorHelper::getVisitorId(request()));
    // PRC-840 — branded storefront'ta chat kapalı (ana site ajanları/markası sızar)
    $chatOnBrandedSite = app(\App\Services\Store\BrandContext::class)->isBranded();
    $chatVisible = $chatEnabled && ($chatIsClient || $chatGuestEnabled) && ! $chatBlocked && ! $chatOnBrandedSite;
    $chatPosition = setting('chat_position', 'bottom-right');
@endphp
@if($chatVisible)
    <div id="chatWidgetRoot" class="chat-widget-root chat-widget-{{ $chatPosition }}" data-position="{{ $chatPosition }}" data-mode="{{ $chatMode }}">
        <button type="button" id="chatWidgetBubble" class="chat-widget-bubble" aria-label="{{ __('store/chat.open') }}">
            <i class="fas fa-comments"></i>
        </button>

        <div id="chatWidgetWindow" class="chat-widget-window" hidden>
            <div class="chat-widget-header">
                <div class="chat-widget-title">{{ __('store/chat.title') }}</div>
                <div class="chat-widget-header-actions">
                    <button type="button" id="chatWidgetEnd" class="chat-widget-refresh" aria-label="{{ __('store/chat.end') }}" title="{{ __('store/chat.end') }}">
                        <i class="fas fa-sign-out-alt"></i>
                    </button>
                    <button type="button" id="chatWidgetRefresh" class="chat-widget-refresh" aria-label="{{ __('store/chat.refresh') }}">
                        <i class="fas fa-sync-alt"></i>
                    </button>
                    <button type="button" id="chatWidgetClose" class="chat-widget-close" aria-label="{{ __('store/chat.close') }}">&times;</button>
                </div>
            </div>

            <form id="chatWidgetPrechat" class="chat-widget-prechat p-3" hidden>
                <div class="mb-2">
                    <input type="text" id="chatPrechatName" class="form-control form-control-sm"
                           maxlength="255" placeholder="{{ __('store/chat.your_name') }}" required>
                </div>
                <div class="mb-2">
                    <input type="email" id="chatPrechatEmail" class="form-control form-control-sm"
                           maxlength="255" placeholder="{{ __('store/chat.your_email') }}" required>
                </div>
                <button type="submit" class="btn btn-sm btn-primary w-100">{{ __('store/chat.start_chat') }}</button>
            </form>

            <div id="chatWidgetMessages" class="chat-widget-messages">
                <div class="chat-widget-empty text-muted small p-3 text-center">
                    {{ __('store/chat.empty') }}
                </div>
            </div>

            <form id="chatWidgetForm" class="chat-widget-form">
                <input type="text" id="chatWidgetInput" class="form-control form-control-sm"
                       maxlength="2000" placeholder="{{ __('store/chat.placeholder') }}" required>
                <button type="submit" class="btn btn-sm btn-primary chat-widget-send" aria-label="{{ __('store/chat.send') }}">
                    <i class="fas fa-paper-plane"></i>
                </button>
            </form>

            <div id="chatWidgetClosed" class="chat-widget-closed p-3 text-center" hidden>
                <div class="small text-muted mb-2">{{ __('store/chat.ended') }}</div>

                <div id="chatWidgetRate" hidden>
                    <div class="small mb-2">{{ __('store/chat.rate_prompt') }}</div>
                    <div id="chatWidgetStars" class="chat-widget-stars mb-2" style="font-size:1.4rem;cursor:pointer;">
                        <i class="far fa-star" data-v="1"></i><i class="far fa-star" data-v="2"></i><i class="far fa-star" data-v="3"></i><i class="far fa-star" data-v="4"></i><i class="far fa-star" data-v="5"></i>
                    </div>
                    <textarea id="chatWidgetRateComment" class="form-control form-control-sm mb-2" rows="2" maxlength="500" placeholder="{{ __('store/chat.rate_comment') }}"></textarea>
                    <button type="button" id="chatWidgetRateSubmit" class="btn btn-sm btn-primary w-100">{{ __('store/chat.rate_submit') }}</button>
                    <div id="chatWidgetRateThanks" class="small text-success mt-2" hidden>{{ __('store/chat.rate_thanks') }}</div>
                </div>

                <button type="button" id="chatWidgetNew" class="btn btn-sm btn-outline-primary w-100 mt-2">
                    <i class="fas fa-plus me-1"></i>{{ __('store/chat.new_chat') }}
                </button>
            </div>
        </div>
    </div>

    {{-- Bu partial layout @stack('scripts')'TEN sonra @include edildiği için
         @push('scripts') geç kalır. Doğrudan emit ediyoruz. --}}
    <script>
    window.chatWidgetConfig = {
        mode: '{{ $chatMode }}',
        routes: {
            start:    '{{ route("store.chat.start") }}',
            send:     '{{ url("/chat") }}/:id/send',
            messages: '{{ url("/chat") }}/:id/messages',
            close:    '{{ url("/chat") }}/:id/close',
            rate:     '{{ url("/chat") }}/:id/rate',
        },
        ratingEnabled: {{ setting('chat_rating_enabled', true) ? 'true' : 'false' }},
        agentName: @json(setting('chat_agent_display_name', 'Destek')),
        translations: {
            empty:        '{{ __("store/chat.empty") }}',
            send_failed:  '{{ __("store/chat.send_failed") }}',
            you:          '{{ __("store/chat.you") }}',
            agent:        '{{ __("store/chat.agent") }}',
            refresh:      '{{ __("store/chat.refresh") }}',
            prechat_required: '{{ __("store/chat.prechat_required") }}',
        },
    };
    </script>
    @vite(['resources/css/store/chat-widget.css', 'resources/js/store/chat-widget.js'])
@endif
