@if($activeAnnouncement)
    @php
        $colors = $activeAnnouncement->type_color;
    @endphp

    {{-- Banner Display Mode --}}
    @if(in_array($activeAnnouncement->display_mode, ['banner', 'both']))
        <div
            x-data="{
                visible: !localStorage.getItem('dismissed_announcement_{{ $activeAnnouncement->id }}'),
                dismiss() {
                    localStorage.setItem('dismissed_announcement_{{ $activeAnnouncement->id }}', '1');
                    @if(auth()->check())
                        fetch('{{ route('announcements.dismiss', $activeAnnouncement) }}', {
                            method: 'POST',
                            headers: {
                                'Content-Type': 'application/json',
                                'X-CSRF-TOKEN': '{{ csrf_token() }}'
                            }
                        });
                    @endif
                    this.visible = false;
                }
            }"
            x-show="visible"
            x-cloak
            class="mb-4"
        >
            <div class="bg-gradient-to-r {{ $colors['bg'] }} border-l-4 {{ $colors['border'] }} p-4 shadow-sm">
                <div class="flex justify-between items-start">
                    <div>
                        <h3 class="font-semibold {{ $colors['text'] }}">{{ $activeAnnouncement->title }}</h3>
                        <div class="mt-1 {{ $colors['text'] }} text-sm">
                            {!! $activeAnnouncement->body !!}
                        </div>
                    </div>
                    @if($activeAnnouncement->is_dismissible)
                        <button
                            type="button"
                            @click="dismiss()"
                            class="ml-4 text-gray-500 hover:text-gray-700 focus:outline-none"
                        >
                            <svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20">
                                <path fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd"></path>
                            </svg>
                        </button>
                    @endif
                </div>
            </div>
        </div>
    @endif

    {{-- Modal Display Mode --}}
    @if(in_array($activeAnnouncement->display_mode, ['modal', 'both']))
        <div
            x-data="{
                init() {
                    if (!localStorage.getItem('dismissed_announcement_{{ $activeAnnouncement->id }}')) {
                        $nextTick(() => {
                            $dispatch('open-modal', 'announcement-{{ $activeAnnouncement->id }}');
                        });
                    }
                }
            }"
        >
            <x-modal name="announcement-{{ $activeAnnouncement->id }}" maxWidth="md">
                <div class="p-6">
                    <div class="flex items-start justify-between mb-4">
                        <h3 class="text-lg font-semibold text-gray-900">{{ $activeAnnouncement->title }}</h3>
                        <button
                            type="button"
                            @click="$dispatch('close-modal', 'announcement-{{ $activeAnnouncement->id }}')"
                            class="text-gray-500 hover:text-gray-700 focus:outline-none"
                        >
                            <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
                            </svg>
                        </button>
                    </div>

                    <div class="prose prose-sm max-w-none mb-6 text-gray-700">
                        {!! $activeAnnouncement->body !!}
                    </div>

                    <div class="flex justify-end gap-3">
                        @if($activeAnnouncement->is_dismissible)
                            <button
                                type="button"
                                @click="
                                    localStorage.setItem('dismissed_announcement_{{ $activeAnnouncement->id }}', '1');
                                    @if(auth()->check())
                                        fetch('{{ route('announcements.dismiss', $activeAnnouncement) }}', {
                                            method: 'POST',
                                            headers: {
                                                'Content-Type': 'application/json',
                                                'X-CSRF-TOKEN': '{{ csrf_token() }}'
                                            }
                                        });
                                    @endif
                                    $dispatch('close-modal', 'announcement-{{ $activeAnnouncement->id }}');
                                "
                                class="px-4 py-2 bg-gray-300 text-gray-800 rounded-md hover:bg-gray-400 transition-colors"
                            >
                                Got it
                            </button>
                        @endif
                    </div>
                </div>
            </x-modal>
        </div>
    @endif
@endif
