{{--
    Discussion Card Partial
    =======================
    Reusable card for displaying discussion summaries on public pages.
    RBAC: edit/delete buttons only shown to authorised users.

    Variables:
    - $discussion: Discussion model instance
    - $featured: bool (optional)
--}}
<div class="bg-theme-card rounded-2xl shadow-sm hover:shadow-md transition-shadow overflow-hidden
    {{ $featured ?? false ? 'border-l-4 border-yellow-500' : '' }}
    {{ $discussion->is_pinned ? 'border-l-4 border-purple-500' : '' }}">
    <div class="p-5">

        {{-- Header: topic badge + status badges --}}
        <div class="flex items-center flex-wrap gap-2 mb-3">
            @if($discussion->topic)
                <a href="{{ route('public.discussions.topic', $discussion->topic->slug) }}"
                   class="inline-flex items-center gap-1 px-2.5 py-0.5 rounded-full text-xs font-semibold
                          {{ $discussion->topic->color ? "bg-{$discussion->topic->color}-100 text-{$discussion->topic->color}-800" : 'bg-teal-100 text-teal-800' }}
                          hover:opacity-80 transition">
                    <i class="fas {{ $discussion->topic->icon ?? 'fa-folder' }} text-xs"></i>
                    {{ $discussion->topic->name }}
                </a>
            @endif

            @if($featured ?? false)
                <span class="inline-flex items-center gap-1 px-2 py-0.5 rounded text-xs font-medium bg-yellow-100 text-yellow-800">
                    <i class="fas fa-star text-xs"></i>Featured
                </span>
            @endif

            @if($discussion->is_pinned)
                <span class="inline-flex items-center gap-1 px-2 py-0.5 rounded text-xs font-medium bg-purple-100 text-purple-800">
                    <i class="fas fa-thumbtack text-xs"></i>Pinned
                </span>
            @endif
        </div>

        {{-- Title --}}
        <h3 class="text-base font-bold text-theme-heading mb-2 line-clamp-2">
            <a href="{{ route('public.discussions.show', $discussion->slug) }}"
               class="hover:text-teal-600 transition">
                {{ $discussion->title }}
            </a>
        </h3>

        {{-- Excerpt --}}
        <p class="text-theme-body text-sm mb-4 line-clamp-2">
            {{ Str::limit(strip_tags($discussion->content), 160) }}
        </p>

        {{-- Inward divider --}}
        <div class="border-t border-gray-200 mb-3" style="box-shadow: inset 0 1px 3px rgba(0,0,0,0.05);"></div>

        {{-- Footer: author + stats + actions --}}
        <div class="flex items-center justify-between gap-2 flex-wrap">

            {{-- Author --}}
            <div class="flex items-center gap-2">
                <img src="{{ $discussion->user->avatar ?? 'https://ui-avatars.com/api/?name=' . urlencode($discussion->user->name) . '&size=32&background=0d9488&color=fff' }}"
                     alt="{{ $discussion->user->name }}"
                     class="w-7 h-7 rounded-full">
                <div>
                    <p class="text-xs font-semibold text-theme-heading leading-none">{{ $discussion->user->name }}</p>
                    <p class="text-xs text-gray-400 mt-0.5">{{ $discussion->created_at->diffForHumans() }}</p>
                </div>
            </div>

            {{-- Stats --}}
            <div class="flex items-center gap-3 text-xs text-theme-body">
                <span class="flex items-center gap-1" title="Views">
                    <i class="fas fa-eye text-teal-400"></i>
                    {{ number_format($discussion->view_count) }}
                </span>
                <span class="flex items-center gap-1" title="Replies">
                    <i class="fas fa-comment text-emerald-400"></i>
                    {{ $discussion->active_replies_count ?? $discussion->reply_count }}
                </span>
                @if(($discussion->like_count ?? 0) > 0)
                    <span class="flex items-center gap-1 text-teal-600" title="Likes">
                        <i class="fas fa-thumbs-up"></i>
                        {{ $discussion->like_count }}
                    </span>
                @endif
            </div>

            {{-- RBAC action buttons --}}
            @auth
                <div class="flex items-center gap-1">
                    @if($discussion->user_id === auth()->id() || auth()->user()->can('edit discussions'))
                        <a href="{{ route('knowledge-sharing.discussions.edit', $discussion) }}"
                           class="p-1.5 text-amber-500 hover:bg-amber-50 rounded-lg transition text-xs" title="Edit">
                            <i class="fas fa-edit"></i>
                        </a>
                    @endif
                    @if($discussion->user_id === auth()->id() || auth()->user()->can('delete discussions'))
                        <form action="{{ route('knowledge-sharing.discussions.destroy', $discussion) }}"
                              method="POST" class="inline"
                              onsubmit="return confirm('Delete this discussion?')">
                            @csrf
                            @method('DELETE')
                            <button type="submit" class="p-1.5 text-red-400 hover:bg-red-50 rounded-lg transition text-xs" title="Delete">
                                <i class="fas fa-trash"></i>
                            </button>
                        </form>
                    @endif
                </div>
            @endauth
        </div>
    </div>
</div>
