{{--
    Public Knowledge Base Search Page
    ==================================
    Displays search results for knowledge base questions.
    
    Features:
    - Search results with highlighting
    - Filter options
    - Relevance ranking
    
    Layout: Site Layout (public)
--}}
<x-site-layout 
    title="{{ 'Search: ' . $query . ' - Knowledge Base' }}"
    meta-description="Search results for {{ $query }} in our knowledge base"
>
    {{-- Hero Section --}}
    <div class="bg-gradient-to-r from-blue-600 to-blue-700 shadow-lg">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
            
            {{-- Search Header --}}
            <div class="mb-6">
                <h1 class="text-3xl font-bold text-white mb-2">Search Knowledge Base</h1>
                <p class="text-blue-100">Find answers, guides, and documentation</p>
            </div>
            
            {{-- Search Form --}}
            <form method="GET" action="{{ route('public.knowledge-base.search') }}" class="max-w-3xl">
                <div class="relative">
                    <input type="text" 
                           name="q" 
                           value="{{ $query }}"
                           placeholder="Search for answers..."
                           class="w-full pl-12 pr-4 py-4 text-lg border-0 rounded-xl shadow-lg focus:ring-2 focus:ring-blue-300">
                    <i class="fas fa-search absolute left-4 top-1/2 -translate-y-1/2 text-gray-400 text-xl"></i>
                </div>
            </form>
            
            {{-- Results Count --}}
            @if($query)
                <p class="mt-4 text-blue-100">
                    Found <strong>{{ $questions->total() }}</strong> results for "<em>{{ $query }}</em>"
                </p>
            @endif
        </div>
    </div>

    {{-- Breadcrumb --}}
    <div class="bg-gray-50 border-b border-gray-200">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-3">
            <nav class="flex items-center space-x-2 text-sm text-gray-600" aria-label="Breadcrumb">
                <a href="{{ url('/') }}" class="hover:text-blue-600 transition flex items-center">
                    <i class="fas fa-home mr-1"></i>Home
                </a>
                <i class="fas fa-chevron-right text-gray-400 text-xs"></i>
                <a href="{{ route('public.knowledge-base.index') }}" class="hover:text-blue-600 transition">Knowledge Base</a>
                <i class="fas fa-chevron-right text-gray-400 text-xs"></i>
                <span class="text-gray-900 font-medium">Search Results</span>
            </nav>
        </div>
    </div>

    {{-- Main Content --}}
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
        <div class="grid grid-cols-1 lg:grid-cols-4 gap-8">
            
            {{-- Main Results --}}
            <div class="lg:col-span-3">
                
                {{-- Filter Options --}}
                <div class="bg-theme-card rounded-lg shadow-sm p-4 mb-6">
                    <div class="flex flex-wrap items-center gap-4">
                        <span class="text-sm text-theme-body">Filter by:</span>
                        
                        <select name="category" 
                                onchange="this.form.submit()"
                                form="filterForm"
                                class="px-3 py-1.5 border theme-divider rounded-lg bg-theme-body text-theme-body text-sm">
                            <option value="">All Categories</option>
                            @foreach($categories as $category)
                                <option value="{{ $category->id }}" {{ request('category') == $category->id ? 'selected' : '' }}>
                                    {{ $category->name }}
                                </option>
                            @endforeach
                        </select>
                        
                        <select name="difficulty" 
                                onchange="this.form.submit()"
                                form="filterForm"
                                class="px-3 py-1.5 border theme-divider rounded-lg bg-theme-body text-theme-body text-sm">
                            <option value="">All Levels</option>
                            <option value="beginner" {{ request('difficulty') === 'beginner' ? 'selected' : '' }}>Beginner</option>
                            <option value="intermediate" {{ request('difficulty') === 'intermediate' ? 'selected' : '' }}>Intermediate</option>
                            <option value="advanced" {{ request('difficulty') === 'advanced' ? 'selected' : '' }}>Advanced</option>
                        </select>
                        
                        <select name="sort" 
                                onchange="this.form.submit()"
                                form="filterForm"
                                class="px-3 py-1.5 border theme-divider rounded-lg bg-theme-body text-theme-body text-sm">
                            <option value="relevance" {{ request('sort') === 'relevance' ? 'selected' : '' }}>Relevance</option>
                            <option value="recent" {{ request('sort') === 'recent' ? 'selected' : '' }}>Recent</option>
                            <option value="popular" {{ request('sort') === 'popular' ? 'selected' : '' }}>Popular</option>
                        </select>
                    </div>
                    
                    <form id="filterForm" method="GET" action="{{ route('public.knowledge-base.search') }}" class="hidden">
                        <input type="hidden" name="q" value="{{ $query }}">
                    </form>
                </div>
                
                {{-- Results List --}}
                @if($questions->isNotEmpty())
                    <div class="space-y-4">
                        @foreach($questions as $question)
                            <div class="bg-theme-card rounded-lg shadow-sm p-4 hover:shadow-md transition-shadow">
                                {{-- Category Badge --}}
                                @if($question->category)
                                    <div class="mb-2">
                                        <a href="{{ route('public.knowledge-base.category', $question->category->slug) }}" 
                                           class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium {{ $question->category->color ? "bg-{$question->category->color}-100 text-{$question->category->color}-800" : 'bg-blue-100 text-blue-800' }}">
                                            <i class="fas {{ $question->category->icon ?? 'fa-folder' }} mr-1"></i>
                                            {{ $question->category->name }}
                                        </a>
                                    </div>
                                @endif
                                
                                {{-- Title --}}
                                <h3 class="text-lg font-semibold text-theme-heading mb-2">
                                    <a href="{{ route('public.knowledge-base.show', $question->slug) }}" 
                                       class="hover:text-blue-600 transition">
                                        {!! preg_replace('/(' . preg_quote($query, '/') . ')/i', '<mark class="bg-yellow-200">$1</mark>', $question->title) !!}
                                    </a>
                                </h3>
                                
                                {{-- Content Preview --}}
                                <p class="text-theme-body text-sm mb-3 line-clamp-2">
                                    {!! preg_replace('/(' . preg_quote($query, '/') . ')/i', '<mark class="bg-yellow-200">$1</mark>', Str::limit(strip_tags($question->content), 200)) !!}
                                </p>
                                
                                {{-- Meta --}}
                                <div class="flex items-center justify-between">
                                    <div class="flex items-center text-sm text-theme-body">
                                        <img src="{{ $question->user->avatar ?? 'https://ui-avatars.com/api/?name=' . urlencode($question->user->name) . '&size=24' }}" 
                                             alt="{{ $question->user->name }}"
                                             class="w-6 h-6 rounded-full mr-2">
                                        <span>{{ $question->user->name }}</span>
                                        <span class="mx-2">•</span>
                                        <span>{{ $question->created_at->diffForHumans() }}</span>
                                    </div>
                                    <div class="flex items-center space-x-3 text-sm text-theme-body">
                                        <span title="Views">
                                            <i class="fas fa-eye mr-1"></i>
                                            {{ number_format($question->view_count) }}
                                        </span>
                                        <span title="Answers" class="{{ $question->has_accepted_answer ? 'text-green-600' : '' }}">
                                            <i class="fas {{ $question->has_accepted_answer ? 'fa-check-circle' : 'fa-comment' }} mr-1"></i>
                                            {{ $question->answers_count ?? 0 }}
                                        </span>
                                    </div>
                                </div>
                            </div>
                        @endforeach
                    </div>
                    
                    {{-- Pagination --}}
                    @if($questions->hasPages())
                        <div class="mt-6">
                            {{ $questions->links() }}
                        </div>
                    @endif
                @else
                    <div class="bg-theme-card rounded-lg shadow-sm p-8 text-center">
                        <i class="fas fa-search text-4xl text-gray-300 mb-4"></i>
                        <h3 class="text-lg font-medium text-theme-heading mb-2">No results found</h3>
                        <p class="text-theme-body mb-4">
                            @if($query)
                                No questions match "{{ $query }}". Try different keywords or check your spelling.
                            @else
                                Enter a search term to find answers.
                            @endif
                        </p>
                        <div class="flex flex-wrap justify-center gap-2">
                            <a href="{{ route('public.knowledge-base.index') }}" 
                               class="inline-flex items-center px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition">
                                <i class="fas fa-list mr-2"></i>
                                Browse All Questions
                            </a>
                        </div>
                    </div>
                @endif
            </div>
            
            {{-- Sidebar --}}
            <div class="lg:col-span-1 space-y-6">
                
                {{-- Categories Sidebar --}}
                <div class="bg-theme-card rounded-lg shadow-sm p-4">
                    <h3 class="text-lg font-semibold text-theme-heading mb-4 flex items-center">
                        <i class="fas fa-folder text-blue-600 mr-2"></i>
                        Categories
                    </h3>
                    <ul class="space-y-2">
                        @foreach($categories as $category)
                            <li>
                                <a href="{{ route('public.knowledge-base.search', ['q' => $query, 'category' => $category->id]) }}" 
                                   class="flex items-center justify-between p-2 rounded-lg hover:bg-theme-body transition {{ request('category') == $category->id ? 'bg-blue-50 text-blue-700' : '' }}">
                                    <span class="flex items-center">
                                        <i class="fas {{ $category->icon ?? 'fa-folder' }} {{ $category->color ? "text-{$category->color}-500" : 'text-gray-400' }} mr-2"></i>
                                        {{ $category->name }}
                                    </span>
                                </a>
                            </li>
                        @endforeach
                    </ul>
                </div>
                
                {{-- Popular Tags --}}
                <div class="bg-theme-card rounded-lg shadow-sm p-4">
                    <h3 class="text-lg font-semibold text-theme-heading mb-3 flex items-center">
                        <i class="fas fa-tags text-blue-600 mr-2"></i>
                        Popular Tags
                    </h3>
                    <div class="flex flex-wrap gap-2">
                        @foreach($tags as $tag => $count)
                            @if($count > 0)
                                <a href="{{ route('public.knowledge-base.tag', $tag) }}" 
                                   class="px-3 py-1 bg-theme-body text-theme-body rounded-full text-sm hover:bg-blue-100 hover:text-blue-700 transition">
                                    {{ $tag }}
                                </a>
                            @endif
                        @endforeach
                    </div>
                </div>
                
                {{-- Search Tips --}}
                <div class="bg-theme-card rounded-lg shadow-sm p-4">
                    <h3 class="text-lg font-semibold text-theme-heading mb-3 flex items-center">
                        <i class="fas fa-lightbulb text-yellow-500 mr-2"></i>
                        Search Tips
                    </h3>
                    <ul class="text-sm text-theme-body space-y-2">
                        <li class="flex items-start">
                            <i class="fas fa-check text-green-500 mr-2 mt-1"></i>
                            Use specific keywords for better results
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check text-green-500 mr-2 mt-1"></i>
                            Try different word variations
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check text-green-500 mr-2 mt-1"></i>
                            Filter by category to narrow results
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</x-site-layout>
