{{--
    Public Discussion Search Page
    ==============================
    Displays search results for discussions.
    
    Features:
    - Search results with highlighting
    - Filter options
    - Relevance ranking
    
    Layout: Site Layout (public)
--}}
<x-site-layout 
    title="{{ 'Search: ' . $query . ' - Discussions' }}"
    meta-description="Search results for {{ $query }} in community discussions"
>
    {{-- Hero Section --}}
    <div class="bg-gradient-to-r from-green-600 to-green-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 Discussions</h1>
                <p class="text-green-100">Find answers and conversations in our community</p>
            </div>
            
            {{-- Search Form --}}
            <form method="GET" action="{{ route('public.discussions.search') }}" class="max-w-3xl">
                <div class="relative">
                    <input type="text" 
                           name="q" 
                           value="{{ $query }}"
                           placeholder="Search discussions..."
                           class="w-full pl-12 pr-4 py-4 text-lg border-0 rounded-xl shadow-lg focus:ring-2 focus:ring-green-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-green-100">
                    Found <strong>{{ $discussions->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-green-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.discussions.index') }}" class="hover:text-green-600 transition">Discussions</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="topic" 
                                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 Topics</option>
                            @foreach($topics as $topic)
                                <option value="{{ $topic->id }}" {{ request('topic') == $topic->id ? 'selected' : '' }}>
                                    {{ $topic->name }}
                                </option>
                            @endforeach
                        </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="latest" {{ request('sort') === 'latest' ? 'selected' : '' }}>Latest</option>
                            <option value="popular" {{ request('sort') === 'popular' ? 'selected' : '' }}>Most Popular</option>
                        </select>
                    </div>
                    
                    <form id="filterForm" method="GET" action="{{ route('public.discussions.search') }}" class="hidden">
                        <input type="hidden" name="q" value="{{ $query }}">
                    </form>
                </div>
                
                {{-- Results List --}}
                @if($discussions->isNotEmpty())
                    <div class="space-y-4">
                        @foreach($discussions as $discussion)
                            <div class="bg-theme-card rounded-lg shadow-sm p-4 hover:shadow-md transition-shadow">
                                {{-- Topic Badge --}}
                                @if($discussion->topic)
                                    <div class="mb-2">
                                        <a href="{{ route('public.discussions.topic', $discussion->topic->slug) }}" 
                                           class="text-xs font-medium {{ $discussion->topic->color ? "text-{$discussion->topic->color}-600" : 'text-gray-500' }} hover:underline">
                                            <i class="fas {{ $discussion->topic->icon ?? 'fa-folder' }} mr-1"></i>
                                            {{ $discussion->topic->name }}
                                        </a>
                                    </div>
                                @endif
                                
                                {{-- Title --}}
                                <h3 class="text-lg font-semibold text-theme-heading mb-2">
                                    <a href="{{ route('public.discussions.show', $discussion->slug) }}" 
                                       class="hover:text-green-600 transition">
                                        {!! preg_replace('/(' . preg_quote($query, '/') . ')/i', '<mark class="bg-yellow-200">$1</mark>', $discussion->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($discussion->content), 200)) !!}
                                </p>
                                
                                {{-- Meta --}}
                                <div class="flex items-center justify-between text-sm">
                                    <div class="flex items-center space-x-3 text-theme-body">
                                        <img src="{{ $discussion->user->avatar ?? 'https://ui-avatars.com/api/?name=' . urlencode($discussion->user->name) . '&size=24' }}" 
                                             alt="{{ $discussion->user->name }}"
                                             class="w-6 h-6 rounded-full">
                                        <span>{{ $discussion->user->name }}</span>
                                        <span>•</span>
                                        <span>{{ $discussion->created_at->diffForHumans() }}</span>
                                    </div>
                                    <div class="flex items-center space-x-3 text-theme-body">
                                        <span title="Views">
                                            <i class="fas fa-eye mr-1"></i>
                                            {{ number_format($discussion->view_count) }}
                                        </span>
                                        <span title="Replies">
                                            <i class="fas fa-comment mr-1"></i>
                                            {{ $discussion->active_replies_count ?? 0 }}
                                        </span>
                                    </div>
                                </div>
                            </div>
                        @endforeach
                    </div>
                    
                    {{-- Pagination --}}
                    @if($discussions->hasPages())
                        <div class="mt-6">
                            {{ $discussions->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 discussions match "{{ $query }}". Try different keywords or check your spelling.
                            @else
                                Enter a search term to find discussions.
                            @endif
                        </p>
                        <div class="flex flex-wrap justify-center gap-2">
                            <a href="{{ route('public.discussions.index') }}" 
                               class="inline-flex items-center px-4 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 transition">
                                <i class="fas fa-list mr-2"></i>
                                Browse All Discussions
                            </a>
                            @auth
                                <a href="{{ route('knowledge-sharing.discussions.create') }}" 
                                   class="inline-flex items-center px-4 py-2 border theme-divider rounded-lg hover:bg-theme-body transition">
                                    <i class="fas fa-plus mr-2"></i>
                                    Start a Discussion
                                </a>
                            @endauth
                        </div>
                    </div>
                @endif
            </div>
            
            {{-- Sidebar --}}
            <div class="lg:col-span-1 space-y-6">
                
                {{-- Topics 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-green-600 mr-2"></i>
                        Topics
                    </h3>
                    <ul class="space-y-2">
                        @foreach($topics as $topic)
                            <li>
                                <a href="{{ route('public.discussions.search', ['q' => $query, 'topic' => $topic->id]) }}" 
                                   class="flex items-center justify-between p-2 rounded-lg hover:bg-theme-body transition {{ request('topic') == $topic->id ? 'bg-green-50 text-green-700' : '' }}">
                                    <span class="flex items-center">
                                        <i class="fas {{ $topic->icon ?? 'fa-folder' }} {{ $topic->color ? "text-{$topic->color}-500" : 'text-gray-400' }} mr-2"></i>
                                        {{ $topic->name }}
                                    </span>
                                </a>
                            </li>
                        @endforeach
                    </ul>
                </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 topic to narrow results
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</x-site-layout>
