<x-app-layout>

    <x-module-hero
        title="Campaign: {{ Str::limit($campaign->title, 55) }}"
        icon="fa-bullhorn">

        <x-slot name="backButton">
            <a href="{{ route('advocacy.campaigns.index') }}"
               class="inline-flex items-center gap-1 text-sm text-[#475569] hover:text-[#1e293b] transition-colors">
                <i class="fas fa-arrow-left text-xs"></i> Back to Campaigns
            </a>
        </x-slot>

        <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>

        @can('edit campaigns')
        <x-slot name="actions">
            <a href="{{ route('advocacy.campaigns.edit', $campaign) }}"
               class="inline-flex items-center gap-2 px-4 py-2 bg-white text-green-800 text-sm font-semibold rounded-lg hover:bg-green-50 transition">
                <i class="fas fa-edit"></i> Edit
            </a>
        </x-slot>
        @endcan

    </x-module-hero>

    <div class="p-6">

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

        <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">

            {{-- Main Content --}}
            <div class="lg:col-span-2 space-y-5">

                {{-- Campaign Header --}}
                <div class="bg-white rounded-lg border border-gray-200 overflow-hidden">
                    @if($campaign->banner_image)
                    <div class="h-40 bg-gray-900">
                        <img src="{{ asset('storage/' . $campaign->banner_image) }}"
                             alt="{{ $campaign->title }}"
                             class="w-full h-full object-cover opacity-80">
                    </div>
                    @endif
                    <div class="p-5">
                        <div class="flex items-start justify-between gap-4">
                            <div>
                                <div class="flex items-center gap-2 mb-2">
                                    @php
                                    $statusColors = [
                                        'draft'     => 'bg-gray-100 text-gray-700',
                                        'active'    => 'bg-green-100 text-green-700',
                                        'paused'    => 'bg-yellow-100 text-yellow-700',
                                        'completed' => 'bg-blue-100 text-blue-700',
                                    ];
                                    @endphp
                                    <span class="px-2 py-1 text-xs font-semibold rounded-full {{ $statusColors[$campaign->status] ?? 'bg-gray-100 text-gray-700' }}">
                                        {{ ucfirst($campaign->status) }}
                                    </span>
                                    @if($campaign->is_featured)
                                    <span class="text-yellow-500 text-sm"><i class="fas fa-star"></i></span>
                                    @endif
                                </div>
                                <h1 class="text-xl font-bold text-gray-900">{{ $campaign->title }}</h1>
                            </div>
                            @can('edit campaigns')
                            <button onclick="toggleStatus()"
                                    class="px-3 py-1.5 bg-blue-100 text-blue-700 rounded-lg hover:bg-blue-200 transition text-xs font-medium flex-shrink-0">
                                <i class="fas fa-power-off mr-1"></i> Toggle Status
                            </button>
                            @endcan
                        </div>
                        <p class="text-gray-600 mt-3 text-sm">{{ $campaign->description }}</p>
                        @if($campaign->objectives)
                        <div class="mt-4 p-3 bg-teal-50 rounded-lg">
                            <h4 class="font-semibold text-teal-900 mb-1 text-sm">Objectives</h4>
                            <p class="text-teal-800 text-sm whitespace-pre-line">{{ $campaign->objectives }}</p>
                        </div>
                        @endif
                    </div>
                </div>

                {{-- Analytics --}}
                <div class="bg-white rounded-lg border border-gray-200 p-5">
                    <h3 class="text-base font-semibold text-gray-800 mb-4 flex items-center gap-2">
                        <i class="fas fa-chart-bar text-teal-600"></i> Campaign Analytics
                    </h3>
                    <div class="grid grid-cols-2 md:grid-cols-4 gap-4">
                        <div class="text-center p-3 bg-gray-50 rounded-lg">
                            <p class="text-2xl font-bold text-teal-600">{{ $analytics['views'] }}</p>
                            <p class="text-xs text-gray-600 mt-1">Total Views</p>
                        </div>
                        <div class="text-center p-3 bg-gray-50 rounded-lg">
                            <p class="text-2xl font-bold text-rose-600">{{ $analytics['stories'] }}</p>
                            <p class="text-xs text-gray-600 mt-1">Stories</p>
                        </div>
                        <div class="text-center p-3 bg-gray-50 rounded-lg">
                            <p class="text-2xl font-bold text-yellow-600">{{ $analytics['pending_stories'] }}</p>
                            <p class="text-xs text-gray-600 mt-1">Pending Stories</p>
                        </div>
                        <div class="text-center p-3 bg-gray-50 rounded-lg">
                            <p class="text-2xl font-bold text-purple-600">{{ $analytics['polls'] }}</p>
                            <p class="text-xs text-gray-600 mt-1">Active Polls</p>
                        </div>
                    </div>
                </div>

                {{-- Stories --}}
                @if($campaign->stories->count() > 0)
                <div class="bg-white rounded-lg border border-gray-200 p-5">
                    <div class="flex items-center justify-between mb-4">
                        <h3 class="text-base font-semibold text-gray-800 flex items-center gap-2">
                            <i class="fas fa-heart text-rose-500"></i> Associated Stories
                        </h3>
                        <span class="text-xs text-gray-500">{{ $campaign->stories->count() }} stories</span>
                    </div>
                    <div class="space-y-2">
                        @foreach($campaign->stories as $story)
                        <div class="flex items-center justify-between p-3 bg-gray-50 rounded-lg">
                            <div>
                                <p class="font-medium text-gray-900 text-sm">{{ $story->title }}</p>
                                <p class="text-xs text-gray-500">by {{ $story->author_name }}</p>
                            </div>
                            <span class="px-2 py-0.5 text-xs rounded-full {{ $story->status === 'published' ? 'bg-green-100 text-green-700' : 'bg-yellow-100 text-yellow-700' }}">
                                {{ ucfirst($story->status) }}
                            </span>
                        </div>
                        @endforeach
                    </div>
                </div>
                @endif

            </div>

            {{-- Sidebar --}}
            <div class="space-y-5">

                {{-- Quick Actions --}}
                <div class="bg-white rounded-lg border border-gray-200 p-5">
                    <h3 class="text-base font-semibold text-gray-800 mb-3">Quick Actions</h3>
                    <div class="space-y-2">
                        <a href="{{ route('advocacy.public.campaigns.show', $campaign) }}" target="_blank"
                           class="block w-full px-4 py-2 bg-teal-100 text-teal-700 rounded-lg hover:bg-teal-200 transition text-sm text-left">
                            <i class="fas fa-external-link-alt mr-2"></i> View Public Page
                        </a>
                        @can('create polls')
                        <a href="{{ route('advocacy.polls.create', ['campaign' => $campaign->id]) }}"
                           class="block w-full px-4 py-2 bg-purple-100 text-purple-700 rounded-lg hover:bg-purple-200 transition text-sm text-left">
                            <i class="fas fa-poll mr-2"></i> Create Poll
                        </a>
                        @endcan
                        @can('edit campaigns')
                        <a href="{{ route('advocacy.campaigns.edit', $campaign) }}"
                           class="block w-full px-4 py-2 bg-amber-100 text-amber-700 rounded-lg hover:bg-amber-200 transition text-sm text-left">
                            <i class="fas fa-edit mr-2"></i> Edit Campaign
                        </a>
                        @endcan
                        @can('delete campaigns')
                        <form action="{{ route('advocacy.campaigns.destroy', $campaign) }}" method="POST"
                              onsubmit="return confirm('Delete this campaign?')">
                            @csrf @method('DELETE')
                            <button type="submit"
                                    class="w-full px-4 py-2 bg-red-100 text-red-700 rounded-lg hover:bg-red-200 transition text-sm text-left">
                                <i class="fas fa-trash mr-2"></i> Delete Campaign
                            </button>
                        </form>
                        @endcan
                    </div>
                </div>

                {{-- Campaign Details --}}
                <div class="bg-white rounded-lg border border-gray-200 p-5">
                    <h3 class="text-base font-semibold text-gray-800 mb-3">Details</h3>
                    <ul class="space-y-2 text-sm">
                        <li class="flex justify-between py-1.5 border-b border-gray-100">
                            <span class="text-gray-500">Status</span>
                            <span class="font-medium">{{ ucfirst($campaign->status) }}</span>
                        </li>
                        <li class="flex justify-between py-1.5 border-b border-gray-100">
                            <span class="text-gray-500">Start Date</span>
                            <span class="font-medium">{{ $campaign->start_date->format('M d, Y') }}</span>
                        </li>
                        @if($campaign->end_date)
                        <li class="flex justify-between py-1.5 border-b border-gray-100">
                            <span class="text-gray-500">End Date</span>
                            <span class="font-medium">{{ $campaign->end_date->format('M d, Y') }}</span>
                        </li>
                        @endif
                        @if($campaign->target_audience)
                        <li class="flex justify-between py-1.5 border-b border-gray-100">
                            <span class="text-gray-500">Audience</span>
                            <span class="font-medium text-right max-w-[60%]">{{ $campaign->target_audience }}</span>
                        </li>
                        @endif
                        <li class="flex justify-between py-1.5">
                            <span class="text-gray-500">Created</span>
                            <span class="font-medium">{{ $campaign->created_at->format('M d, Y') }}</span>
                        </li>
                    </ul>
                </div>

                {{-- Active Polls --}}
                @if($campaign->polls->count() > 0)
                <div class="bg-white rounded-lg border border-gray-200 p-5">
                    <h3 class="text-base font-semibold text-gray-800 mb-3 flex items-center gap-2">
                        <i class="fas fa-poll text-purple-600"></i> Polls
                    </h3>
                    <div class="space-y-2">
                        @foreach($campaign->polls as $poll)
                        <a href="{{ route('advocacy.polls.show', $poll) }}"
                           class="block p-3 bg-gray-50 rounded-lg hover:bg-gray-100 transition">
                            <p class="font-medium text-gray-900 text-sm line-clamp-1">{{ $poll->question }}</p>
                            <p class="text-xs text-gray-500 mt-0.5">{{ $poll->total_responses }} responses</p>
                        </a>
                        @endforeach
                    </div>
                </div>
                @endif

            </div>
        </div>
    </div>

</x-app-layout>

@push('scripts')
<script>
function toggleStatus() {
    fetch('{{ route('advocacy.campaigns.toggle-status', $campaign) }}', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content }
    }).then(r => r.json()).then(d => { if (d.success) window.location.reload(); });
}
</script>
@endpush
