<x-app-layout>

    <x-module-hero
        title="Poll: {{ Str::limit($poll->question, 55) }}"
        description="{{ Str::limit($poll->question, 60) }}"
        icon="fa-poll">

        <x-slot name="backButton">
            <a href="{{ route('advocacy.polls.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 Polls
            </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>

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

                {{-- Question Card --}}
                <div class="bg-white rounded-lg border border-gray-200 p-5">
                    <div class="flex items-center gap-2 mb-3">
                        @php
                        $statusColors = [
                            'draft'  => 'bg-gray-100 text-gray-700',
                            'active' => 'bg-green-100 text-green-700',
                            'closed' => 'bg-blue-100 text-blue-700',
                        ];
                        @endphp
                        <span class="px-2 py-1 text-xs font-semibold rounded-full {{ $statusColors[$poll->status] ?? 'bg-gray-100' }}">
                            {{ ucfirst($poll->status) }}
                        </span>
                        @if($poll->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 mb-2">{{ $poll->question }}</h1>
                    @if($poll->description)
                    <p class="text-gray-600 text-sm">{{ $poll->description }}</p>
                    @endif
                </div>

                {{-- Results --}}
                <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-purple-600"></i>
                        Results ({{ $poll->total_responses }} total votes)
                    </h3>
                    <div class="space-y-4">
                        @foreach($results as $option)
                        <div>
                            <div class="flex justify-between mb-1">
                                <span class="text-sm font-medium text-gray-700">{{ $option['text'] }}</span>
                                <span class="text-sm text-gray-500">{{ $option['votes'] }} votes ({{ $option['percentage'] }}%)</span>
                            </div>
                            <div class="w-full bg-gray-200 rounded-full h-3 overflow-hidden">
                                <div class="h-full rounded-full transition-all duration-500"
                                     style="width: {{ $option['percentage'] }}%; background-color: {{ $option['color'] ?? '#8B5CF6' }}">
                                </div>
                            </div>
                        </div>
                        @endforeach
                    </div>
                </div>

                {{-- Recent Responses --}}
                @if($poll->responses->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-history text-purple-600"></i> Recent Responses
                    </h3>
                    <div class="space-y-1">
                        @foreach($poll->responses->take(10) as $response)
                        <div class="flex items-center justify-between py-2 border-b border-gray-100">
                            <p class="text-sm text-gray-700">
                                @if($response->user)
                                    {{ $response->user->name }}
                                @else
                                    <span class="text-gray-400">Anonymous</span>
                                @endif
                            </p>
                            <p class="text-xs text-gray-400">{{ $response->created_at->diffForHumans() }}</p>
                        </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">
                        @can('edit polls')
                        @if($poll->status === 'draft')
                        <a href="{{ route('advocacy.polls.edit', $poll) }}"
                           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 Poll
                        </a>
                        @endif
                        @if($poll->status === 'active')
                        <button onclick="closePoll()"
                                class="w-full px-4 py-2 bg-blue-100 text-blue-700 rounded-lg hover:bg-blue-200 transition text-sm text-left">
                            <i class="fas fa-stop mr-2"></i> Close Poll
                        </button>
                        @endif
                        @endcan
                        @if($poll->status === 'draft')
                        <p class="text-xs text-gray-500 p-3 bg-gray-50 rounded">
                            <i class="fas fa-info-circle mr-1"></i>
                            Draft polls must be activated to receive votes.
                        </p>
                        @endif
                        @can('delete polls')
                        <form action="{{ route('advocacy.polls.destroy', $poll) }}" method="POST"
                              onsubmit="return confirm('Delete this poll?')">
                            @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 Poll
                            </button>
                        </form>
                        @endcan
                    </div>
                </div>

                {{-- Poll 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">Type</span>
                            <span class="font-medium">{{ $poll->poll_type === 'single_choice' ? 'Single Choice' : 'Multiple Choice' }}</span>
                        </li>
                        <li class="flex justify-between py-1.5 border-b border-gray-100">
                            <span class="text-gray-500">Display</span>
                            <span class="font-medium capitalize">{{ $poll->display_position }}</span>
                        </li>
                        <li class="flex justify-between py-1.5 border-b border-gray-100">
                            <span class="text-gray-500">Show Results</span>
                            <span class="font-medium">{{ $poll->show_results ? 'Yes' : 'No' }}</span>
                        </li>
                        @if($poll->ends_at)
                        <li class="flex justify-between py-1.5 border-b border-gray-100">
                            <span class="text-gray-500">Ends</span>
                            <span class="font-medium">{{ $poll->ends_at->format('M d, Y H:i') }}</span>
                        </li>
                        @endif
                        @if($poll->campaign)
                        <li class="py-1.5">
                            <span class="text-gray-500 block mb-1">Campaign</span>
                            <a href="{{ route('advocacy.campaigns.show', $poll->campaign) }}"
                               class="text-teal-600 hover:text-teal-800 text-sm">
                                {{ $poll->campaign->title }}
                            </a>
                        </li>
                        @endif
                    </ul>
                </div>

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

</x-app-layout>

@push('scripts')
<script>
function closePoll() {
    if (!confirm('Close this poll? No more votes will be accepted.')) return;
    fetch('{{ route('advocacy.polls.close', $poll) }}', {
        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
