<x-app-layout>

    <x-module-hero
        title="Health Stories"
        description="Review and approve submitted health stories"
        icon="fa-heart">

        <x-slot name="tabs">
            @can('view campaigns')
            <a href="{{ route('advocacy.campaigns.index') }}"
               class="module-hero-tab {{ request()->routeIs('advocacy.campaigns.*') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                      inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                      md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-bullhorn text-sm"></i> Campaigns
            </a>
            @endcan

            @can('view polls')
            <a href="{{ route('advocacy.polls.index') }}"
               class="module-hero-tab {{ request()->routeIs('advocacy.polls.*') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                      inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                      md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-poll text-sm"></i> Polls
            </a>
            @endcan

            @can('view stories')
            <a href="{{ route('advocacy.stories.index') }}"
               class="module-hero-tab {{ request()->routeIs('advocacy.stories.*') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                      inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                      md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-newspaper text-sm"></i> Health Stories
            </a>
            @endcan
        </x-slot>

    </x-module-hero>

    <div class="w-full">

        @if(session('success'))
        <div class="mx-6 mt-4 p-4 bg-green-100 border border-green-400 text-green-700 rounded-lg flex items-center text-sm">
            <i class="fas fa-check-circle mr-3"></i> {{ session('success') }}
        </div>
        @endif

        {{-- Stats Cards --}}
        <div class="p-4 grid grid-cols-2 md:grid-cols-4 gap-4 bg-gray-50 border-b border-gray-200">
            <div class="bg-white rounded-lg border border-gray-200 p-4">
                <p class="text-xs text-gray-500 font-semibold uppercase tracking-wide">Pending</p>
                <p class="text-2xl font-bold text-blue-600 mt-1">{{ $stats['pending'] }}</p>
            </div>
            <div class="bg-white rounded-lg border border-gray-200 p-4">
                <p class="text-xs text-gray-500 font-semibold uppercase tracking-wide">Approved</p>
                <p class="text-2xl font-bold text-green-600 mt-1">{{ $stats['approved'] }}</p>
            </div>
            <div class="bg-white rounded-lg border border-gray-200 p-4">
                <p class="text-xs text-gray-500 font-semibold uppercase tracking-wide">Rejected</p>
                <p class="text-2xl font-bold text-red-600 mt-1">{{ $stats['rejected'] }}</p>
            </div>
            <div class="bg-white rounded-lg border border-gray-200 p-4">
                <p class="text-xs text-gray-500 font-semibold uppercase tracking-wide">Total</p>
                <p class="text-2xl font-bold text-gray-900 mt-1">{{ $stats['total'] }}</p>
            </div>
        </div>

        {{-- Filter Bar --}}
        <div class="p-4 bg-white border-b border-gray-200">
            <form method="GET" class="flex flex-wrap gap-3 items-end">
                <div>
                    <label class="block text-xs font-medium text-gray-600 mb-1">Status</label>
                    <select name="status" class="px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-rose-500">
                        <option value="pending_review" {{ ($filters['status'] ?? 'pending_review') == 'pending_review' ? 'selected' : '' }}>Pending Review</option>
                        <option value="approved"       {{ ($filters['status'] ?? '') == 'approved'       ? 'selected' : '' }}>Approved</option>
                        <option value="published"      {{ ($filters['status'] ?? '') == 'published'      ? 'selected' : '' }}>Published</option>
                        <option value="rejected"       {{ ($filters['status'] ?? '') == 'rejected'       ? 'selected' : '' }}>Rejected</option>
                        <option value=""               {{ !($filters['status'] ?? null)                  ? 'selected' : '' }}>All Status</option>
                    </select>
                </div>
                <button type="submit" class="px-4 py-2 bg-rose-700 text-white text-sm rounded-lg hover:bg-rose-800 transition">
                    <i class="fas fa-filter mr-1"></i> Filter
                </button>
            </form>
        </div>

        {{-- Table --}}
        <div class="overflow-x-auto">
            <table class="w-full table-zebra">
                <thead>
                    <tr>
                        <th class="px-4 py-3 text-left th-col-1">Story</th>
                        <th class="px-4 py-3 text-left th-col-2">Author</th>
                        <th class="px-4 py-3 text-left th-col-3">Campaign</th>
                        <th class="px-4 py-3 text-left th-col-4">Status</th>
                        <th class="px-4 py-3 text-left th-col-5">Submitted</th>
                        <th class="px-4 py-3 text-center th-col-6">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($stories as $story)
                    <tr class="odd:bg-white even:bg-gray-50 hover:bg-gray-100 transition-colors border-b border-gray-100 last:border-0">
                        <td class="px-4 py-3">
                            <div class="flex items-center gap-3">
                                @if($story->featured_image)
                                <img src="{{ asset('storage/' . $story->featured_image) }}"
                                     class="w-10 h-10 rounded object-cover flex-shrink-0">
                                @else
                                <div class="w-10 h-10 bg-rose-100 rounded flex items-center justify-center flex-shrink-0">
                                    <i class="fas fa-heart text-rose-500 text-sm"></i>
                                </div>
                                @endif
                                <div>
                                    <p class="font-medium text-gray-900 text-sm line-clamp-1">{{ $story->title }}</p>
                                    <p class="text-xs text-gray-500 line-clamp-1">{{ Str::limit(strip_tags($story->content), 50) }}</p>
                                </div>
                            </div>
                        </td>
                        <td class="px-4 py-3">
                            <p class="text-sm text-gray-900">{{ $story->author_name }}</p>
                            <p class="text-xs text-gray-500">{{ $story->author_email }}</p>
                        </td>
                        <td class="px-4 py-3 text-sm text-gray-700">{{ $story->campaign?->title ?? '—' }}</td>
                        <td class="px-4 py-3">
                            @php
                            $statusColors = [
                                'pending_review' => 'bg-blue-100 text-blue-700',
                                'approved'       => 'bg-green-100 text-green-700',
                                'published'      => 'bg-purple-100 text-purple-700',
                                'rejected'       => 'bg-red-100 text-red-700',
                            ];
                            @endphp
                            <span class="px-2 py-1 text-xs font-semibold rounded-full {{ $statusColors[$story->status] ?? 'bg-gray-100 text-gray-700' }}">
                                {{ str_replace('_', ' ', ucfirst($story->status)) }}
                            </span>
                        </td>
                        <td class="px-4 py-3 text-sm text-gray-500">{{ $story->created_at->diffForHumans() }}</td>
                        <td class="px-4 py-3">
                            <div class="flex items-center justify-center gap-1 flex-wrap">
                                @can('moderate stories')
                                <a href="{{ route('advocacy.stories.moderate', $story) }}" class="btn-action-view">
                                    <i class="fas fa-eye"></i> Review
                                </a>
                                @endcan
                                @can('feature stories')
                                <button onclick="toggleFeatured({{ $story->id }})"
                                        class="inline-flex items-center gap-1 px-3 py-1.5 text-xs font-medium text-white rounded {{ $story->is_featured ? 'bg-yellow-500 hover:bg-yellow-600' : 'bg-gray-400 hover:bg-gray-500' }} transition-colors"
                                        title="{{ $story->is_featured ? 'Unfeature' : 'Feature' }}">
                                    <i class="fas fa-star"></i>
                                </button>
                                @endcan
                                @can('delete stories')
                                <form action="{{ route('advocacy.stories.destroy', $story) }}" method="POST" class="inline"
                                      onsubmit="return confirm('Delete this story?')">
                                    @csrf @method('DELETE')
                                    <button type="submit" class="btn-action-delete">
                                        <i class="fas fa-trash"></i>
                                    </button>
                                </form>
                                @endcan
                            </div>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="6" class="px-6 py-12 text-center">
                            <i class="fas fa-heart text-4xl text-gray-300 mb-3"></i>
                            <p class="text-gray-500 text-sm">No stories to moderate.</p>
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        @if($stories->hasPages())
        <div class="px-4 py-3 border-t border-gray-200 bg-white">
            {{ $stories->withQueryString()->links() }}
        </div>
        @endif

    </div>

</x-app-layout>

@push('scripts')
<script>
function toggleFeatured(storyId) {
    fetch(`/dashboard/advocacy/stories/${storyId}/toggle-featured`, {
        method: 'POST',
        headers: { 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content }
    }).then(r => r.json()).then(d => { if (d.success) window.location.reload(); });
}
</script>
@endpush
