@props([
    'publications' => collect(),
    'title'        => 'Resource Repository',
    'role'         => 'partner',   // 'partner' | 'contributor'
])

<div class="bg-white/80 backdrop-blur-sm rounded-xl shadow-lg border border-indigo-100 overflow-hidden">

    <div class="flex items-center justify-between px-5 pt-5 pb-3 border-b border-indigo-50">
        <div class="flex items-center gap-3">
            <span class="text-indigo-500 text-xl">📚</span>
            <h3 class="text-sm font-semibold text-gray-700 uppercase tracking-wide">{{ $title }}</h3>
        </div>
        @if($role === 'contributor')
            @can('create publications')
                <a href="{{ route('research.create') }}"
                   class="inline-flex items-center gap-1 text-xs font-medium text-indigo-600 hover:text-indigo-800 transition-colors">
                    + Submit New Publication
                </a>
            @endcan
        @endif
    </div>

    <div class="px-5 py-4">
        @forelse($publications as $pub)
            <div class="mb-4 last:mb-0 border border-gray-100 rounded-lg p-4"
                 x-data="{ previewing: false }">

                <div class="flex items-start justify-between gap-3">
                    <div class="flex-1 min-w-0">
                        <p class="font-semibold text-gray-800 text-sm truncate">{{ $pub->title }}</p>
                        <p class="text-xs text-gray-500 mt-0.5">
                            {{ $pub->authors ?? 'Unknown author' }}
                            @if($pub->approved_at)
                                · {{ \Carbon\Carbon::parse($pub->approved_at)->format('M Y') }}
                            @endif
                        </p>
                    </div>

                    <div class="flex items-center gap-2 flex-shrink-0">
                        @if($role === 'contributor')
                            {{-- Status badge --}}
                            @php
                                $statusColors = [
                                    'published' => 'bg-green-100 text-green-700',
                                    'pending'   => 'bg-amber-100 text-amber-700',
                                    'draft'     => 'bg-gray-100 text-gray-600',
                                    'rejected'  => 'bg-red-100 text-red-700',
                                ];
                                $sc = $statusColors[$pub->status ?? 'draft'] ?? 'bg-gray-100 text-gray-600';
                            @endphp
                            <span class="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium {{ $sc }}">
                                {{ ucfirst($pub->status ?? 'draft') }}
                            </span>
                            @can('edit publications')
                                <a href="{{ route('research.edit', $pub) }}"
                                   class="text-xs text-indigo-600 hover:text-indigo-800 font-medium transition-colors">
                                    Edit
                                </a>
                            @endcan
                        @endif
                    </div>
                </div>

                {{-- Files --}}
                @if($pub->files && $pub->files->count())
                    <div class="mt-3 flex flex-wrap gap-2">
                        @foreach($pub->files->take(3) as $file)
                            <div class="flex items-center gap-1">
                                @if($role === 'partner')
                                    <a href="{{ route('research.files.download', $file) }}"
                                       class="inline-flex items-center gap-1 text-xs text-indigo-600 hover:text-indigo-800 font-medium transition-colors">
                                        ⬇ {{ $file->original_name ?? 'Download' }}
                                    </a>
                                @endif

                                @if(str_ends_with(strtolower($file->file_path ?? ''), '.pdf'))
                                    <button @click="previewing = !previewing"
                                            class="text-xs text-gray-500 hover:text-gray-700 transition-colors">
                                        <span x-text="previewing ? '▲ Close' : '👁 Preview'"></span>
                                    </button>
                                @endif
                            </div>
                        @endforeach
                    </div>

                    {{-- PDF Preview Modal --}}
                    @php $pdfFile = $pub->files->first(fn($f) => str_ends_with(strtolower($f->file_path ?? ''), '.pdf')); @endphp
                    @if($pdfFile)
                        <div x-show="previewing"
                             x-transition:enter="transition ease-out duration-200"
                             x-transition:enter-start="opacity-0 scale-95"
                             x-transition:enter-end="opacity-100 scale-100"
                             x-cloak
                             class="mt-3">
                            <iframe src="{{ Storage::url($pdfFile->file_path) }}"
                                    class="w-full h-64 rounded border border-gray-200"
                                    title="{{ $pub->title }}">
                            </iframe>
                        </div>
                    @endif
                @endif
            </div>
        @empty
            <div class="text-center py-8">
                <p class="text-gray-400 text-sm">No publications available.</p>
                @if($role === 'contributor')
                    @can('create publications')
                        <a href="{{ route('research.create') }}"
                           class="mt-2 inline-block text-xs text-indigo-600 hover:text-indigo-800 font-medium transition-colors">
                            Submit your first publication →
                        </a>
                    @endcan
                @endif
            </div>
        @endforelse
    </div>
</div>
