<?php
/**
 * Notifications - Admin Page
 */

$page_title = 'Notifications';
require_once 'includes/header.php';

try {
    $pdo = getDBConnection();

    // Mark as read if ID provided
    if (isset($_GET['read'])) {
        $stmt = $pdo->prepare("UPDATE notifications SET is_read = 1 WHERE id = ? AND recipient_type = 'admin'");
        $stmt->execute([$_GET['read']]);
    }

    // Mark all as read
    if (isset($_POST['mark_all_read'])) {
        $stmt = $pdo->prepare("UPDATE notifications SET is_read = 1 WHERE recipient_type = 'admin' AND (user_id = ? OR user_id IS NULL)");
        $stmt->execute([$_SESSION['admin_id']]);
    }

    // Get notifications for admins
    // Since we sometimes notify "all admins", user_id might be null or specific admin id
    $stmt = $pdo->prepare("
        SELECT * FROM notifications
        WHERE recipient_type = 'admin' AND (user_id = ? OR user_id IS NULL)
        ORDER BY created_at DESC
        LIMIT 50
    ");
    $stmt->execute([$_SESSION['admin_id']]);
    $notifications = $stmt->fetchAll();

} catch (PDOException $e) {
    $notifications = [];
    $error = "Database error: " . $e->getMessage();
}
?>

<div class="flex h-screen overflow-hidden">
    <?php include 'includes/sidebar.php'; ?>

    <div class="flex-1 flex flex-col overflow-hidden">
        <header class="sticky top-0 z-30 flex items-center bg-white/80 dark:bg-background-dark/80 backdrop-blur-md p-4 justify-between border-b border-slate-200 dark:border-slate-800 lg:px-8">
            <div class="flex items-center gap-4">
                <button class="text-slate-900 dark:text-white flex size-10 items-center justify-center hover:bg-slate-100 dark:hover:bg-slate-800 rounded-full lg:hidden" onclick="toggleSidebar()">
                    <span class="material-symbols-outlined">menu</span>
                </button>
                <h2 class="text-slate-900 dark:text-white text-clamp-lg font-bold">Notifications</h2>
            </div>
            <?php if (!empty($notifications)): ?>
            <form method="POST">
                <button type="submit" name="mark_all_read" class="text-xs font-bold text-primary hover:underline">Mark all as read</button>
            </form>
            <?php endif; ?>
        </header>

        <main class="flex-1 overflow-y-auto p-4 lg:p-8 pb-24 admin-content">
            <div class="max-w-3xl mx-auto space-y-4">
                <?php if (empty($notifications)): ?>
                    <div class="bg-white dark:bg-surface-dark rounded-2xl border border-slate-200 dark:border-slate-800 p-12 text-center">
                        <div class="size-16 bg-slate-100 dark:bg-slate-800 rounded-full flex items-center justify-center mx-auto mb-4">
                            <span class="material-symbols-outlined text-slate-400 text-3xl">notifications_off</span>
                        </div>
                        <p class="text-slate-500 font-medium">No notifications yet.</p>
                    </div>
                <?php else: ?>
                    <?php foreach ($notifications as $n):
                        $icon = 'notifications';
                        $color = 'text-blue-500 bg-blue-50 dark:bg-blue-900/20';

                        if ($n['type'] === 'payment') {
                            $icon = 'payments';
                            $color = 'text-emerald-500 bg-emerald-50 dark:bg-emerald-900/20';
                        } elseif ($n['type'] === 'booking') {
                            $icon = 'event_available';
                            $color = 'text-blue-500 bg-blue-50 dark:bg-blue-900/20';
                        } elseif ($n['type'] === 'error') {
                            $icon = 'error';
                            $color = 'text-rose-500 bg-rose-50 dark:bg-rose-900/20';
                        }
                    ?>
                        <div class="bg-white dark:bg-surface-dark rounded-2xl border <?= $n['is_read'] ? 'border-slate-200 dark:border-slate-800 opacity-75' : 'border-primary shadow-sm ring-1 ring-primary/10' ?> p-4 transition-all">
                            <div class="flex gap-4">
                                <div class="size-10 shrink-0 rounded-full flex items-center justify-center <?= $color ?>">
                                    <span class="material-symbols-outlined text-xl"><?= $icon ?></span>
                                </div>
                                <div class="flex-1">
                                    <div class="flex justify-between items-start">
                                        <h3 class="font-bold text-sm <?= $n['is_read'] ? 'text-slate-700 dark:text-slate-300' : 'text-slate-900 dark:text-white' ?>"><?= htmlspecialchars($n['title']) ?></h3>
                                        <span class="text-[10px] text-slate-400"><?= date('M d, H:i', strtotime($n['created_at'])) ?></span>
                                    </div>
                                    <p class="text-xs text-slate-500 dark:text-slate-400 mt-1"><?= htmlspecialchars($n['message']) ?></p>

                                    <div class="flex gap-4 mt-3">
                                        <?php if ($n['link']): ?>
                                            <a href="<?= htmlspecialchars($n['link']) ?>&read=<?= $n['id'] ?>" class="text-[10px] font-bold text-primary hover:underline flex items-center gap-1">
                                                View Details <span class="material-symbols-outlined text-[12px]">open_in_new</span>
                                            </a>
                                        <?php endif; ?>

                                        <?php if (!$n['is_read']): ?>
                                            <a href="?read=<?= $n['id'] ?>" class="text-[10px] font-bold text-slate-400 hover:text-slate-600">Mark as read</a>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
        </main>
    </div>
</div>

<?php include 'includes/footer.php'; ?>
