@if($threads->count() > 0)
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
        <div class="overflow-x-auto">
            <table class="w-full">
                <thead>
                    <tr class="bg-gray-50 border-b border-gray-200">
                        <th class="px-4 py-3 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider w-1/2">Topic</th>
                        <th class="px-4 py-3 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider">Category</th>
                        <th class="px-4 py-3 text-center text-xs font-semibold text-gray-600 uppercase tracking-wider">Replies</th>
                        <th class="px-4 py-3 text-center text-xs font-semibold text-gray-600 uppercase tracking-wider">Views</th>
                        <th class="px-4 py-3 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider">Last Activity</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($threads as $thread)
                        <tr class="forum-row odd:bg-white even:bg-gray-50 hover:bg-indigo-50/60 transition cursor-pointer border-b border-gray-100 last:border-0"
                            data-thread-id="{{ $thread->id }}"
                            data-thread-url="{{ route('partners.forum.show', $thread) }}"
                            data-is-pinned="{{ $thread->is_pinned ? '1' : '0' }}"
                            data-is-read="{{ isset($thread->is_read_by_user) && $thread->is_read_by_user ? '1' : '0' }}"
                            onclick="window.location.href='{{ route('partners.forum.show', $thread) }}'">

                            <td class="px-4 py-3">
                                <div class="flex items-start gap-3">
                                    <div class="flex-shrink-0 mt-0.5 thread-status-icon">
                                        @if($thread->is_pinned)
                                            <i class="fas fa-thumbtack text-indigo-500 text-sm" title="Pinned"></i>
                                        @elseif($thread->is_locked)
                                            <i class="fas fa-lock text-gray-400 text-sm" title="Locked"></i>
                                        @else
                                            @if(isset($thread->is_read_by_user) && !$thread->is_read_by_user)
                                                <i class="fas fa-circle text-indigo-500 text-xs" title="Unread"></i>
                                            @else
                                                <i class="fas fa-comment text-gray-300 text-sm"></i>
                                            @endif
                                        @endif
                                    </div>
                                    <div class="min-w-0">
                                        <a href="{{ route('partners.forum.show', $thread) }}"
                                           class="font-medium text-gray-900 hover:text-indigo-600 text-sm {{ $thread->is_pinned ? 'font-semibold' : '' }}"
                                           onclick="event.stopPropagation()">
                                            {{ $thread->title }}
                                            @if($thread->is_featured)
                                                <i class="fas fa-star text-yellow-500 ml-1 text-xs"></i>
                                            @endif
                                        </a>
                                        <div class="mt-0.5 flex items-center gap-2 text-xs text-gray-500">
                                            <span>by {{ $thread->user->name }}</span>
                                            @if($thread->partner)
                                                <span class="text-indigo-600">({{ $thread->partner->organization_name }})</span>
                                            @endif
                                            <span>· {{ $thread->created_at->diffForHumans() }}</span>
                                        </div>
                                        @if(!empty($thread->tags) && count($thread->tags) > 0)
                                            <div class="mt-1.5 flex flex-wrap gap-1">
                                                @foreach($thread->tags as $tag)
                                                    <span class="px-1.5 py-0.5 bg-gray-100 text-gray-600 text-xs rounded">#{{ $tag }}</span>
                                                @endforeach
                                            </div>
                                        @endif
                                    </div>
                                </div>
                            </td>

                            <td class="px-4 py-3 whitespace-nowrap">
                                <span class="px-2 py-0.5 text-xs rounded-full bg-indigo-100 text-indigo-700">
                                    {{ $thread->category_label }}
                                </span>
                            </td>

                            <td class="px-4 py-3 whitespace-nowrap text-center text-sm text-gray-700">
                                {{ $thread->replies_count }}
                            </td>

                            <td class="px-4 py-3 whitespace-nowrap text-center text-sm text-gray-700">
                                {{ $thread->views_count }}
                            </td>

                            <td class="px-4 py-3 whitespace-nowrap text-sm text-gray-500">
                                @if($thread->last_reply_at)
                                    <p>{{ $thread->last_reply_at->diffForHumans() }}</p>
                                    @if($thread->lastReplyUser)
                                        <p class="text-xs text-gray-400">by {{ $thread->lastReplyUser->name }}</p>
                                    @endif
                                @else
                                    <span class="text-gray-400 text-xs">No replies yet</span>
                                @endif
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>

        @if($threads->hasPages())
            <div class="px-4 py-3 border-t border-gray-200 bg-gray-50">
                {{ $threads->withQueryString()->links() }}
            </div>
        @endif
    </div>
@else
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-12 text-center">
        <i class="fas fa-comments text-5xl text-gray-300 mb-4 block"></i>
        <h3 class="text-lg font-semibold text-gray-700 mb-2">No Discussions Found</h3>
        <p class="text-gray-500 text-sm mb-6">
            @if(request('q') || request('category'))
                Try adjusting your search or filter criteria.
            @else
                Be the first to start a discussion!
            @endif
        </p>
        @can('create forum threads')
            <a href="{{ route('partners.forum.create') }}"
               class="inline-flex items-center gap-2 px-5 py-2.5 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 transition text-sm font-medium">
                <i class="fas fa-plus"></i> Start a Discussion
            </a>
        @endcan
    </div>
@endif
