{# app/templates/admin/notifications.html #}
{% extends "admin/base.html" %}
{% from 'shared/macros/headers.html' import page_header %}
{% from 'shared/macros/alerts.html' import loading_state, error_state %}
{% from 'shared/macros/modals.html' import confirm_modal %}

{% block title %}Notifications{% endblock %}

{% block alpine_data %}adminNotifications(){% endblock %}

{% block content %}
{{ page_header('Notifications & Alerts') }}

{{ loading_state('Loading notifications...') }}

{{ error_state('Error loading notifications') }}

<!-- Stats Cards -->
<div x-show="!loading" class="grid gap-6 mb-8 md:grid-cols-2 xl:grid-cols-4">
    <!-- Card: Unread Notifications -->
    <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div class="p-3 mr-4 text-blue-500 bg-blue-100 rounded-full dark:text-blue-100 dark:bg-blue-500">
            <span x-html="$icon('bell', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
                Unread
            </p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.unread_count || 0">
                0
            </p>
        </div>
    </div>

    <!-- Card: Active Alerts -->
    <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div class="p-3 mr-4 text-orange-500 bg-orange-100 rounded-full dark:text-orange-100 dark:bg-orange-500">
            <span x-html="$icon('exclamation-triangle', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
                Active Alerts
            </p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="alertStats.active_alerts || 0">
                0
            </p>
        </div>
    </div>

    <!-- Card: Critical -->
    <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div class="p-3 mr-4 text-red-500 bg-red-100 rounded-full dark:text-red-100 dark:bg-red-500">
            <span x-html="$icon('x-circle', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
                Critical
            </p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="alertStats.critical_alerts || 0">
                0
            </p>
        </div>
    </div>

    <!-- Card: Resolved Today -->
    <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div class="p-3 mr-4 text-green-500 bg-green-100 rounded-full dark:text-green-100 dark:bg-green-500">
            <span x-html="$icon('check-circle', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
                Resolved Today
            </p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="alertStats.resolved_today || 0">
                0
            </p>
        </div>
    </div>
</div>

<!-- Tabs -->
<div x-show="!loading" class="mb-6">
    <div class="flex border-b border-gray-200 dark:border-gray-700">
        <button
            @click="activeTab = 'notifications'"
            class="px-4 py-2 text-sm font-medium border-b-2 transition-colors"
            :class="activeTab === 'notifications'
                ? 'border-purple-600 text-purple-600 dark:border-purple-400 dark:text-purple-400'
                : 'border-transparent text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200'"
        >
            Notifications
            <span x-show="stats.unread_count > 0"
                  class="ml-2 px-2 py-0.5 text-xs bg-red-100 text-red-600 rounded-full dark:bg-red-600 dark:text-white"
                  x-text="stats.unread_count"></span>
        </button>
        <button
            @click="activeTab = 'alerts'; loadAlerts()"
            class="px-4 py-2 text-sm font-medium border-b-2 transition-colors"
            :class="activeTab === 'alerts'
                ? 'border-purple-600 text-purple-600 dark:border-purple-400 dark:text-purple-400'
                : 'border-transparent text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200'"
        >
            Platform Alerts
            <span x-show="alertStats.active_alerts > 0"
                  class="ml-2 px-2 py-0.5 text-xs bg-orange-100 text-orange-600 rounded-full dark:bg-orange-600 dark:text-white"
                  x-text="alertStats.active_alerts"></span>
        </button>
    </div>
</div>

<!-- Notifications Tab -->
<div x-show="!loading && activeTab === 'notifications'" class="space-y-4">
    <!-- Filters -->
    <div class="flex flex-wrap items-center justify-between gap-4 px-4 py-3 bg-white rounded-lg shadow-md dark:bg-gray-800">
        <div class="flex items-center gap-4">
            <select
                x-model="filters.priority"
                @change="page = 1; loadNotifications()"
                class="px-3 py-2 text-sm border border-gray-300 dark:border-gray-600 rounded-lg focus:border-purple-400 focus:outline-none dark:bg-gray-700 dark:text-gray-300"
            >
                <option value="">All Priorities</option>
                <option value="critical">Critical</option>
                <option value="high">High</option>
                <option value="normal">Normal</option>
                <option value="low">Low</option>
            </select>

            <select
                x-model="filters.is_read"
                @change="page = 1; loadNotifications()"
                class="px-3 py-2 text-sm border border-gray-300 dark:border-gray-600 rounded-lg focus:border-purple-400 focus:outline-none dark:bg-gray-700 dark:text-gray-300"
            >
                <option value="">All Status</option>
                <option value="false">Unread</option>
                <option value="true">Read</option>
            </select>
        </div>

        <button
            x-show="stats.unread_count > 0"
            @click="markAllAsRead()"
            class="px-4 py-2 text-sm font-medium text-purple-600 hover:text-purple-800 dark:text-purple-400"
        >
            Mark all as read
        </button>
    </div>

    <!-- Notifications List -->
    <div class="bg-white rounded-lg shadow-md dark:bg-gray-800 overflow-hidden">
        <template x-if="loadingNotifications && notifications.length === 0">
            <div class="px-4 py-8 text-center text-gray-500 dark:text-gray-400">
                <span x-html="$icon('spinner', 'w-6 h-6 mx-auto mb-2')"></span>
                <p>Loading notifications...</p>
            </div>
        </template>

        <template x-if="!loadingNotifications && notifications.length === 0">
            <div class="px-4 py-12 text-center text-gray-500 dark:text-gray-400">
                <span x-html="$icon('bell', 'w-12 h-12 mx-auto mb-3 text-gray-300')"></span>
                <p class="font-medium">No notifications</p>
                <p class="text-sm mt-1">You're all caught up!</p>
            </div>
        </template>

        <ul class="divide-y divide-gray-200 dark:divide-gray-700">
            <template x-for="notif in notifications" :key="notif.id">
                <li class="hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors"
                    :class="notif.is_read ? 'opacity-60' : ''">
                    <div class="flex items-start px-4 py-4">
                        <!-- Priority indicator -->
                        <div class="flex-shrink-0 mr-4">
                            <span class="inline-flex items-center justify-center w-10 h-10 rounded-full"
                                  :class="{
                                      'bg-red-100 text-red-600 dark:bg-red-900 dark:text-red-300': notif.priority === 'critical',
                                      'bg-orange-100 text-orange-600 dark:bg-orange-900 dark:text-orange-300': notif.priority === 'high',
                                      'bg-blue-100 text-blue-600 dark:bg-blue-900 dark:text-blue-300': notif.priority === 'normal',
                                      'bg-gray-100 text-gray-600 dark:bg-gray-700 dark:text-gray-300': notif.priority === 'low'
                                  }">
                                <span x-html="getNotificationIcon(notif.type)"></span> <!-- noqa: SEC015 sanitized: app-controlled JS method -->
                            </span>
                        </div>

                        <!-- Content -->
                        <div class="flex-1 min-w-0">
                            <div class="flex items-center justify-between">
                                <p class="text-sm font-semibold text-gray-900 dark:text-gray-100" x-text="notif.title"></p>
                                <span class="text-xs text-gray-400" x-text="formatDate(notif.created_at)"></span>
                            </div>
                            <p class="text-sm text-gray-600 dark:text-gray-400 mt-1" x-text="notif.message"></p>
                            <div class="flex items-center gap-4 mt-2">
                                <span class="inline-flex items-center px-2 py-0.5 text-xs font-medium rounded"
                                      :class="{
                                          'bg-red-100 text-red-800 dark:bg-red-900 dark:text-red-200': notif.priority === 'critical',
                                          'bg-orange-100 text-orange-800 dark:bg-orange-900 dark:text-orange-200': notif.priority === 'high',
                                          'bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-200': notif.priority === 'normal',
                                          'bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-200': notif.priority === 'low'
                                      }"
                                      x-text="notif.priority"></span>
                                <span class="text-xs text-gray-500" x-text="notif.type.replace('_', ' ')"></span>
                            </div>
                        </div>

                        <!-- Actions -->
                        <div class="flex items-center gap-2 ml-4">
                            <template x-if="notif.action_url">
                                <a :href="notif.action_url"
                                   class="px-3 py-1 text-xs font-medium text-purple-600 bg-purple-100 rounded hover:bg-purple-200 dark:bg-purple-900 dark:text-purple-300">
                                    View
                                </a>
                            </template>
                            <template x-if="!notif.is_read">
                                <button @click="markAsRead(notif)"
                                        class="px-3 py-1 text-xs font-medium text-gray-600 bg-gray-100 rounded hover:bg-gray-200 dark:bg-gray-700 dark:text-gray-300">
                                    Mark read
                                </button>
                            </template>
                            <button @click="pendingDeleteNotificationId = notif.id; showDeleteNotificationConfirm = true"
                                    class="p-1 text-gray-400 hover:text-red-500 transition-colors">
                                <span x-html="$icon('trash', 'w-4 h-4')"></span>
                            </button>
                        </div>
                    </div>
                </li>
            </template>
        </ul>

        <!-- Pagination -->
        <div x-show="stats.total > limit" class="flex items-center justify-between px-4 py-3 border-t dark:border-gray-700">
            <span class="text-sm text-gray-600 dark:text-gray-400">
                Showing <span x-text="skip + 1"></span>-<span x-text="Math.min(skip + limit, stats.total)"></span> of <span x-text="stats.total"></span>
            </span>
            <div class="flex items-center gap-2">
                <button
                    @click="page--; loadNotifications()"
                    :disabled="page <= 1"
                    class="px-3 py-1 text-sm font-medium text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-md hover:bg-gray-50 dark:hover:bg-gray-600 disabled:opacity-50"
                >
                    <span x-html="$icon('chevron-left', 'w-4 h-4')"></span>
                </button>
                <button
                    @click="page++; loadNotifications()"
                    :disabled="page * limit >= stats.total"
                    class="px-3 py-1 text-sm font-medium text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-md hover:bg-gray-50 dark:hover:bg-gray-600 disabled:opacity-50"
                >
                    <span x-html="$icon('chevron-right', 'w-4 h-4')"></span>
                </button>
            </div>
        </div>
    </div>
</div>

<!-- Alerts Tab -->
<div x-show="!loading && activeTab === 'alerts'" class="space-y-4">
    <!-- Filters -->
    <div class="flex flex-wrap items-center gap-4 px-4 py-3 bg-white rounded-lg shadow-md dark:bg-gray-800">
        <select
            x-model="alertFilters.severity"
            @change="alertPage = 1; loadAlerts()"
            class="px-3 py-2 text-sm border border-gray-300 dark:border-gray-600 rounded-lg focus:border-purple-400 focus:outline-none dark:bg-gray-700 dark:text-gray-300"
        >
            <option value="">All Severities</option>
            <option value="critical">Critical</option>
            <option value="error">Error</option>
            <option value="warning">Warning</option>
            <option value="info">Info</option>
        </select>

        <select
            x-model="alertFilters.is_resolved"
            @change="alertPage = 1; loadAlerts()"
            class="px-3 py-2 text-sm border border-gray-300 dark:border-gray-600 rounded-lg focus:border-purple-400 focus:outline-none dark:bg-gray-700 dark:text-gray-300"
        >
            <option value="">All Status</option>
            <option value="false">Active</option>
            <option value="true">Resolved</option>
        </select>
    </div>

    <!-- Alerts List -->
    <div class="bg-white rounded-lg shadow-md dark:bg-gray-800 overflow-hidden">
        <template x-if="loadingAlerts && alerts.length === 0">
            <div class="px-4 py-8 text-center text-gray-500 dark:text-gray-400">
                <span x-html="$icon('spinner', 'w-6 h-6 mx-auto mb-2')"></span>
                <p>Loading alerts...</p>
            </div>
        </template>

        <template x-if="!loadingAlerts && alerts.length === 0">
            <div class="px-4 py-12 text-center text-gray-500 dark:text-gray-400">
                <span x-html="$icon('shield-check', 'w-12 h-12 mx-auto mb-3 text-gray-300')"></span>
                <p class="font-medium">No alerts</p>
                <p class="text-sm mt-1">All systems are running smoothly</p>
            </div>
        </template>

        <ul class="divide-y divide-gray-200 dark:divide-gray-700">
            <template x-for="alert in alerts" :key="alert.id">
                <li class="hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors"
                    :class="alert.is_resolved ? 'opacity-60' : ''">
                    <div class="flex items-start px-4 py-4">
                        <!-- Severity indicator -->
                        <div class="flex-shrink-0 mr-4">
                            <span class="inline-flex items-center justify-center w-10 h-10 rounded-full"
                                  :class="{
                                      'bg-red-100 text-red-600 dark:bg-red-900 dark:text-red-300': alert.severity === 'critical',
                                      'bg-orange-100 text-orange-600 dark:bg-orange-900 dark:text-orange-300': alert.severity === 'error',
                                      'bg-yellow-100 text-yellow-600 dark:bg-yellow-900 dark:text-yellow-300': alert.severity === 'warning',
                                      'bg-blue-100 text-blue-600 dark:bg-blue-900 dark:text-blue-300': alert.severity === 'info'
                                  }">
                                <span x-html="$icon('exclamation-triangle', 'w-5 h-5')"></span>
                            </span>
                        </div>

                        <!-- Content -->
                        <div class="flex-1 min-w-0">
                            <div class="flex items-center justify-between">
                                <p class="text-sm font-semibold text-gray-900 dark:text-gray-100" x-text="alert.title"></p>
                                <template x-if="alert.occurrence_count > 1">
                                    <span class="px-2 py-0.5 text-xs font-medium bg-gray-100 text-gray-600 rounded dark:bg-gray-700 dark:text-gray-300"
                                          x-text="alert.occurrence_count + 'x'"></span>
                                </template>
                            </div>
                            <p class="text-sm text-gray-600 dark:text-gray-400 mt-1" x-text="alert.description"></p>
                            <div class="flex items-center gap-4 mt-2">
                                <span class="inline-flex items-center px-2 py-0.5 text-xs font-medium rounded uppercase"
                                      :class="{
                                          'bg-red-100 text-red-800 dark:bg-red-900 dark:text-red-200': alert.severity === 'critical',
                                          'bg-orange-100 text-orange-800 dark:bg-orange-900 dark:text-orange-200': alert.severity === 'error',
                                          'bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-200': alert.severity === 'warning',
                                          'bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-200': alert.severity === 'info'
                                      }"
                                      x-text="alert.severity"></span>
                                <span class="text-xs text-gray-500" x-text="alert.alert_type"></span>
                                <span class="text-xs text-gray-400" x-text="'Last: ' + formatDate(alert.last_occurred_at)"></span>
                            </div>
                        </div>

                        <!-- Actions -->
                        <div class="flex items-center gap-2 ml-4">
                            <template x-if="!alert.is_resolved">
                                <button @click="resolveAlert(alert)"
                                        class="px-3 py-1 text-xs font-medium text-green-600 bg-green-100 rounded hover:bg-green-200 dark:bg-green-900 dark:text-green-300">
                                    Resolve
                                </button>
                            </template>
                            <template x-if="alert.is_resolved">
                                <span class="px-3 py-1 text-xs font-medium text-gray-500 bg-gray-100 rounded dark:bg-gray-700">
                                    Resolved
                                </span>
                            </template>
                        </div>
                    </div>
                </li>
            </template>
        </ul>
    </div>
</div>

<!-- Delete Notification Confirm Modal -->
{{ confirm_modal('deleteNotificationConfirm', 'Delete Notification', 'Are you sure you want to delete this notification? This action cannot be undone.', 'deleteNotification(pendingDeleteNotificationId)', 'showDeleteNotificationConfirm', 'Delete', 'Cancel', 'danger') }}
{% endblock %}

{% block extra_scripts %}
<script defer src="{{ url_for('messaging_static', path='admin/js/notifications.js') }}"></script>
{% endblock %}
