<?php
use App\Core\NotificationService;

if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

if (!function_exists('ec_nb_h')) {
    function ec_nb_h($v) {
        return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
    }
}

$userId = (int)($_SESSION['user_id'] ?? 0);
$conferenceId = !empty($_SESSION['conference_id']) ? (int)$_SESSION['conference_id'] : null;

$unreadCount = 0;
$latestNotifications = [];

if ($userId > 0) {
    try {
        $svc = new NotificationService();
        $unreadCount = $svc->unreadCount($userId, $conferenceId);
        $latestNotifications = $svc->latestForUser($userId, $conferenceId, 5);
    } catch (\Throwable $e) {
        $unreadCount = 0;
        $latestNotifications = [];
    }
}
?>

<style>
.ec-notification-bell-wrap {
    position: relative;
    display: inline-block;
    margin-inline: 8px;
}
.ec-notification-bell-btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    border: 1px solid #e5e7eb;
    background: #ffffff;
    color: #0f172a;
    border-radius: 999px;
    padding: 8px 14px;
    text-decoration: none;
    font-weight: 800;
    box-shadow: 0 8px 20px rgba(15,23,42,.08);
}
.ec-notification-bell-btn:hover {
    color: #1d4ed8;
    text-decoration: none;
}
.ec-notification-badge {
    min-width: 22px;
    height: 22px;
    padding: 0 6px;
    border-radius: 999px;
    background: #dc2626;
    color: #fff;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
}
.ec-notification-dropdown {
    display: none;
    position: absolute;
    inset-inline-end: 0;
    top: 110%;
    width: 360px;
    max-width: calc(100vw - 24px);
    background: #fff;
    border: 1px solid #e5e7eb;
    border-radius: 20px;
    box-shadow: 0 20px 50px rgba(15,23,42,.18);
    z-index: 9999;
    overflow: hidden;
}
.ec-notification-bell-wrap:hover .ec-notification-dropdown {
    display: block;
}
.ec-notification-item {
    display: block;
    padding: 12px 14px;
    border-bottom: 1px solid #f1f5f9;
    color: #0f172a;
    text-decoration: none;
}
.ec-notification-item:hover {
    background: #f8fafc;
    color: #1d4ed8;
    text-decoration: none;
}
.ec-notification-title {
    font-weight: 800;
    font-size: 14px;
}
.ec-notification-meta {
    color: #64748b;
    font-size: 12px;
    margin-top: 3px;
}
.ec-notification-unread-dot {
    display: inline-block;
    width: 8px;
    height: 8px;
    background: #dc2626;
    border-radius: 50%;
    margin-inline-end: 6px;
}
</style>

<div class="ec-notification-bell-wrap">
    <a class="ec-notification-bell-btn" href="/easychair/notifications">
        <i class="fa fa-bell"></i>
        <span>Notifications</span>
        <?php if ($unreadCount > 0): ?>
            <span class="ec-notification-badge"><?= (int)$unreadCount ?></span>
        <?php endif; ?>
    </a>

    <div class="ec-notification-dropdown">
        <div style="padding:14px 16px;background:#0f172a;color:#fff;font-weight:900;">
            Latest Notifications
            <?php if ($unreadCount > 0): ?>
                <span style="float:inline-end;background:#dc2626;color:#fff;border-radius:999px;padding:2px 8px;font-size:12px;">
                    <?= (int)$unreadCount ?> unread
                </span>
            <?php endif; ?>
        </div>

        <?php foreach ($latestNotifications as $n): ?>
            <?php
                $url = !empty($n['link_url']) ? (string)$n['link_url'] : '/easychair/notifications';
            ?>
            <a class="ec-notification-item" href="<?= ec_nb_h($url) ?>">
                <div class="ec-notification-title">
                    <?php if (empty($n['is_read'])): ?>
                        <span class="ec-notification-unread-dot"></span>
                    <?php endif; ?>
                    <?= ec_nb_h($n['title'] ?? '') ?>
                </div>
                <div class="ec-notification-meta">
                    <?= ec_nb_h($n['event_key'] ?? '') ?>
                    <?php if (!empty($n['conference_short_name'])): ?>
                        | <?= ec_nb_h($n['conference_short_name']) ?>
                    <?php endif; ?>
                    | <?= ec_nb_h($n['created_at'] ?? '') ?>
                </div>
            </a>
        <?php endforeach; ?>

        <?php if (!$latestNotifications): ?>
            <div style="padding:16px;color:#64748b;text-align:center;">
                No notifications yet.
            </div>
        <?php endif; ?>

        <div style="display:flex;background:#f8fafc;">
            <a href="/easychair/notifications" style="flex:1;padding:12px;text-align:center;font-weight:800;text-decoration:none;">View all</a>
            <a href="/easychair/notification-settings" style="flex:1;padding:12px;text-align:center;font-weight:800;text-decoration:none;border-inline-start:1px solid #e5e7eb;">Settings</a>
        </div>
    </div>
</div>
