<?php
// /elms/coordinator/notifications.php
declare(strict_types=1);

require_once __DIR__ . '/../includes/coordinator_auth.php';
require_once __DIR__ . '/../includes/elms_notifications.php';

$PAGE_TITLE = 'Notifications';
$ACTIVE = 'notifications';

$db = $mysqli ?? ($conn ?? null);

if (!$db instanceof mysqli) {
    http_response_code(500);
    exit('DB connection failed');
}

$db->set_charset('utf8mb4');

if (!function_exists('h')) {
    function h($value): string {
        return htmlspecialchars((string)$value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
    }
}

$coordUserId = (int)(
    $_SESSION['user_id']
    ?? $_SESSION['admin_id']
    ?? $_SESSION['coordinator_id']
    ?? 1
);

if ($coordUserId <= 0) {
    header('Location: /elms/coordinator/login.php');
    exit;
}

if (isset($_GET['mark_all'])) {
    lms_mark_all_notifications_read($db, 'coordinator', $coordUserId);
    header('Location: /elms/coordinator/notifications.php?done=1');
    exit;
}

if (isset($_GET['read'])) {
    $nid = (int)$_GET['read'];
    lms_mark_notification_read($db, 'coordinator', $coordUserId, $nid);

    $go = trim((string)($_GET['go'] ?? ''));
    if ($go !== '') {
        header('Location: ' . $go);
        exit;
    }
}

$notifications = lms_recent_notifications($db, 'coordinator', $coordUserId, 30);
$unreadCount = lms_unread_notification_count($db, 'coordinator', $coordUserId);

require_once __DIR__ . '/../includes/coordinator_layout_top.php';
?>

<style>
    .notif-page-head {
        border: 1px solid #e5e7eb;
        background:
            radial-gradient(circle at top right, rgba(185, 28, 28, .13), transparent 30%),
            linear-gradient(180deg, #ffffff 0%, #f8fafc 100%);
        border-radius: 18px;
        padding: 20px;
        margin-bottom: 18px;
    }

    .notif-title {
        font-size: 26px;
        font-weight: 900;
        letter-spacing: -.03em;
        margin: 0;
        color: #111827;
    }

    .notif-subtitle {
        color: #6b7280;
        margin-top: 4px;
    }

    .notif-card {
        border: 1px solid #e5e7eb;
        border-radius: 16px;
        background: #fff;
        padding: 16px;
        margin-bottom: 12px;
        box-shadow: 0 8px 22px rgba(15, 23, 42, .04);
    }

    .notif-card.unread {
        background: #fff7ed;
        border-color: #fed7aa;
    }

    .notif-icon {
        width: 44px;
        height: 44px;
        border-radius: 14px;
        display: grid;
        place-items: center;
        background: #fee2e2;
        color: #b91c1c;
        flex: 0 0 auto;
        font-size: 18px;
    }

    .notif-empty {
        border: 1px solid #e5e7eb;
        border-radius: 18px;
        background: #fff;
        padding: 50px 20px;
        text-align: center;
        color: #6b7280;
    }
</style>

<div class="notif-page-head d-flex flex-column flex-md-row justify-content-between align-items-md-center gap-3">
    <div>
        <h1 class="notif-title">Notifications</h1>
        <div class="notif-subtitle">
            You have <?= (int)$unreadCount ?> unread notification(s).
        </div>
    </div>

    <div class="d-flex gap-2">
        <a href="/elms/coordinator/notifications.php?mark_all=1"
           class="btn btn-outline-danger rounded-pill fw-bold">
            <i class="bi bi-check2-all me-1"></i>
            Mark All Read
        </a>

        <a href="/elms/coordinator/dashboard.php"
           class="btn btn-light border rounded-pill fw-bold">
            <i class="bi bi-arrow-left me-1"></i>
            Dashboard
        </a>
    </div>
</div>

<?php if (isset($_GET['done'])): ?>
    <div class="alert alert-success rounded-4">
        <i class="bi bi-check-circle me-1"></i>
        Notifications marked as read.
    </div>
<?php endif; ?>

<?php if (!$notifications): ?>
    <div class="notif-empty">
        <i class="bi bi-bell-slash fs-1 d-block mb-2"></i>
        <h5 class="fw-bold">No notifications yet</h5>
        <p class="mb-0">Coordinator ELMS updates will appear here.</p>
    </div>
<?php else: ?>
    <?php foreach ($notifications as $n): ?>
        <?php
            $isUnread = (int)($n['is_read'] ?? 0) === 0;
            $link = trim((string)($n['link_url'] ?? ''));
            if ($link === '') {
                $link = '#';
            }

            $openUrl = '/elms/coordinator/notifications.php?read=' . (int)$n['id'] . '&go=' . urlencode($link);
        ?>

        <div class="notif-card <?= $isUnread ? 'unread' : '' ?>">
            <div class="d-flex gap-3 align-items-start">
                <div class="notif-icon">
                    <i class="bi <?= h($n['icon'] ?: 'bi-bell') ?>"></i>
                </div>

                <div class="flex-grow-1">
                    <div class="d-flex justify-content-between gap-2 align-items-start">
                        <div>
                            <h5 class="fw-bold mb-1"><?= h($n['title'] ?? 'Notification') ?></h5>

                            <?php if (!empty($n['message'])): ?>
                                <div class="text-muted mb-2">
                                    <?= h($n['message']) ?>
                                </div>
                            <?php endif; ?>
                        </div>

                        <?php if ($isUnread): ?>
                            <span class="badge bg-danger rounded-pill">New</span>
                        <?php else: ?>
                            <span class="badge bg-light text-muted border rounded-pill">Read</span>
                        <?php endif; ?>
                    </div>

                    <div class="small text-muted mb-3">
                        <?= h(date('d M Y, h:i A', strtotime((string)$n['created_at']))) ?>
                    </div>

                    <a href="<?= h($openUrl) ?>"
                       class="btn btn-sm btn-danger rounded-pill px-3">
                        <i class="bi bi-box-arrow-up-right me-1"></i>
                        Open
                    </a>
                </div>
            </div>
        </div>
    <?php endforeach; ?>
<?php endif; ?>

<?php require_once __DIR__ . '/../includes/coordinator_layout_bottom.php'; ?>