@php
    $isSupportChatContext = old('source') === 'support_chat' || session('contact_source') === 'support_chat';
@endphp

@if($settings?->support_chat_enabled)
    <div class="ssa-support-chat {{ $isSupportChatContext ? 'is-open' : '' }}" data-support-chat>
        <button
            type="button"
            class="ssa-support-chat__toggle"
            data-support-chat-toggle
            aria-expanded="{{ $isSupportChatContext ? 'true' : 'false' }}"
            aria-label="{{ $settings->support_chat_button_label ?: 'Open support chat' }}"
            @if($isSupportChatContext) hidden @endif
        >
            <span class="ssa-support-chat__toggle-icon" aria-hidden="true">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">
                    <path d="M21 11.5a8.5 8.5 0 0 1-8.5 8.5H7l-4 3v-6.5A8.5 8.5 0 1 1 21 11.5Z"></path>
                </svg>
            </span>
            <span>{{ $settings->support_chat_button_label ?: 'Quick support' }}</span>
        </button>

        <div class="ssa-support-chat__panel" data-support-chat-panel @if(! $isSupportChatContext) hidden @endif>
            <div class="ssa-support-chat__header">
                <div>
                    <p class="ssa-support-chat__eyebrow">Direct support</p>
                    <h3>{{ $settings->support_chat_title ?: 'Need help with tracking?' }}</h3>
                    <p>{{ $settings->support_chat_intro ?: 'Send a quick message and we will follow up using the same email workflow as the contact page.' }}</p>
                </div>
                <button type="button" class="ssa-support-chat__close" data-support-chat-close aria-label="Close support chat">&times;</button>
            </div>

            @if(session('success') && session('contact_source') === 'support_chat')
                <div class="ssa-success-alert">
                    {{ session('success') }}
                </div>
            @endif

            <form action="{{ route('contact.store') }}" method="POST" class="ssa-contact-form ssa-support-chat__form">
                @csrf
                <input type="hidden" name="source" value="support_chat">
                <input type="hidden" name="page_url" value="{{ old('page_url', url()->full()) }}">

                <label>
                    <span>Name</span>
                    <input type="text" name="name" value="{{ old('name') }}" required>
                    @error('name')<p class="ssa-form-error">{{ $message }}</p>@enderror
                </label>

                <label>
                    <span>Email</span>
                    <input type="email" name="email" value="{{ old('email') }}" required>
                    @error('email')<p class="ssa-form-error">{{ $message }}</p>@enderror
                </label>

                <label>
                    <span>Phone</span>
                    <input type="text" name="phone" value="{{ old('phone') }}">
                    @error('phone')<p class="ssa-form-error">{{ $message }}</p>@enderror
                </label>

                <label>
                    <span>Message</span>
                    <textarea name="message" rows="4" required>{{ old('message') }}</textarea>
                    @error('message')<p class="ssa-form-error">{{ $message }}</p>@enderror
                </label>

                <button type="submit" class="ssa-gradient-button ssa-submit-button">
                    {{ $settings->support_chat_button_label ?: 'Send message' }}
                </button>
            </form>
        </div>
    </div>
@endif
