<?php
require_once 'includes/config.php';
require_once 'includes/functions.php';
require_once 'includes/auth.php';
requireUserLogin();
$uid = (int)$_SESSION['user_id'];
$currentUser = currentUser($pdo);

// Mark all read
if (isset($_GET['mark_all'])) {
    $pdo->prepare("UPDATE notifications SET is_read=1,read_at=NOW() WHERE user_id=?")->execute([$uid]);
    header('Location: /notifications.php'); exit;
}
// Mark single read
if (isset($_GET['mark'])) {
    $pdo->prepare("UPDATE notifications SET is_read=1,read_at=NOW() WHERE id=? AND user_id=?")->execute([(int)$_GET['mark'], $uid]);
    header('Location: /notifications.php'); exit;
}

$page = max(1,(int)($_GET['page']??1)); $perPage = 20;
$total = (int)$pdo->prepare("SELECT COUNT(*) FROM notifications WHERE user_id=?")->execute([$uid]) ? (int)$pdo->query("SELECT COUNT(*) FROM notifications WHERE user_id=$uid")->fetchColumn() : 0;
$notifs = $pdo->prepare("SELECT * FROM notifications WHERE user_id=? ORDER BY created_at DESC LIMIT $perPage OFFSET " . ($page-1)*$perPage);
$notifs->execute([$uid]); $list = $notifs->fetchAll();
$unread = (int)$pdo->prepare("SELECT COUNT(*) FROM notifications WHERE user_id=? AND is_read=0")->execute([$uid]) ? (int)$pdo->query("SELECT COUNT(*) FROM notifications WHERE user_id=$uid AND is_read=0")->fetchColumn() : 0;
$pageTitle = 'Notifications';
?>
<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title><?= $pageTitle ?> | <?= SITE_NAME ?></title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
<link rel="stylesheet" href="/assets/css/style.css">
</head><body>
<?php include 'includes/header.php'; ?>
<div class="page-wrapper"><div class="container" style="max-width:760px;">
<div class="d-flex justify-content-between align-items-center mb-3">
  <div class="page-title mb-0"><i class="fa fa-bell me-2 text-danger"></i>Notifications <?php if($unread): ?><span class="badge bg-danger"><?= $unread ?></span><?php endif; ?></div>
  <?php if($unread): ?><a href="?mark_all=1" class="btn btn-sm btn-outline-secondary">Mark all read</a><?php endif; ?>
</div>
<div class="card">
  <?php if (empty($list)): ?>
  <div class="card-body text-center py-5 text-muted"><i class="fa fa-bell-slash fa-3x mb-3 d-block"></i>No notifications yet.</div>
  <?php else: foreach ($list as $n):
    $icons = ['success'=>'check-circle','danger'=>'exclamation-circle','warning'=>'exclamation-triangle','transaction'=>'exchange-alt','info'=>'info-circle'];
    $colors= ['success'=>'text-success','danger'=>'text-danger','warning'=>'text-warning','transaction'=>'text-primary','info'=>'text-info'];
    $icon = $icons[$n['type']] ?? 'bell'; $color = $colors[$n['type']] ?? 'text-secondary';
  ?>
  <div class="d-flex align-items-start gap-3 p-3 border-bottom <?= !$n['is_read']?'bg-light':'' ?>" style="<?= !$n['is_read']?'border-left:3px solid var(--primary)!important;':'' ?>">
    <div class="<?= $color ?>" style="font-size:1.4rem;margin-top:2px;"><i class="fa fa-<?= $icon ?>"></i></div>
    <div class="flex-grow-1">
      <div class="fw-600 <?= !$n['is_read']?'fw-bold':'' ?>"><?= sanitize($n['title']) ?></div>
      <div class="small text-muted mt-1"><?= sanitize($n['message']) ?></div>
      <div class="small text-muted mt-1"><?= timeAgo($n['created_at']) ?></div>
    </div>
    <?php if (!$n['is_read']): ?>
    <a href="?mark=<?= $n['id'] ?>" class="btn btn-xs btn-outline-secondary" title="Mark read"><i class="fa fa-check"></i></a>
    <?php endif; ?>
  </div>
  <?php endforeach; endif; ?>
</div>
</div></div>
<?php include 'includes/footer.php'; ?>
</body></html>
