<?php
session_start();
$page_title = 'Notification';
$active_nav = 'notifications';
include("includes/header.php");
$uid = (int)$_SESSION['user_id'];

$nid = (int)($_GET['id'] ?? 0);

$stmt = $conn->prepare(
    "SELECT cn.*, r.name AS restaurant_name, r.logo AS restaurant_logo
     FROM customer_notifications cn
     LEFT JOIN restaurants r ON cn.restaurant_id = r.id
     WHERE cn.id = ? AND cn.user_id = ? LIMIT 1"
);
$stmt->bind_param("ii", $nid, $uid);
$stmt->execute();
$n = $stmt->get_result()->fetch_assoc();
if (!$n) { header("Location: notifications.php"); exit; }

// Mark as read on open.
if (empty($n['is_read'])) {
    $mr = $conn->prepare("UPDATE customer_notifications SET is_read=1 WHERE id=? AND user_id=?");
    $mr->bind_param("ii", $nid, $uid);
    $mr->execute();
}

// Featured menu item (column exists once a restaurant has featured a dish).
$item = null;
$menu_item_id = (int)($n['menu_item_id'] ?? 0);
if ($menu_item_id) {
    if ($iq = $conn->prepare("SELECT id, name, price, image, description, is_available FROM menu_items WHERE id=? LIMIT 1")) {
        $iq->bind_param("i", $menu_item_id);
        $iq->execute();
        $item = $iq->get_result()->fetch_assoc() ?: null;
    }
}

$rid       = (int)($n['restaurant_id'] ?? 0);
$is_rest   = in_array($n['type'] ?? '', ['promo', 'broadcast'], true) && $rid;
$deep_link = $rid ? ('scan.php?rid=' . $rid . ($item ? '&item=' . (int)$item['id'] : '')) : '';
$source    = $n['restaurant_name'] ?? 'Dabil';
$full_date = date('l, F j, Y \a\t g:i A', strtotime($n['created_at']));

$logo_path = $n['restaurant_logo'] ? '../restaurantadmin/uploads/logos/' . $n['restaurant_logo'] : null;
$has_logo  = $logo_path && file_exists(__DIR__ . '/' . $logo_path);

$img = ($item && $item['image'] && file_exists(__DIR__ . '/../restaurantadmin/uploads/menu/' . $item['image']))
    ? '../restaurantadmin/uploads/menu/' . $item['image'] : '';
?>
<style>
.app-topbar { display: none; }
.main-content { padding-top: calc(env(safe-area-inset-top) + 6px); }
.nd-wrap { max-width: 600px; margin: 0 auto; }
.sub-topbar { display: flex; align-items: center; gap: 12px; padding: 4px 2px 18px; }
.sub-topbar .sub-back { width: 40px; height: 40px; border-radius: 50%; display: flex; align-items: center; justify-content: center; color: var(--primary); background: var(--primary-bg); flex-shrink: 0; text-decoration: none; }
.sub-topbar .sub-title { font-size: 1.15rem; font-weight: 800; letter-spacing: -.02em; color: var(--text); }

.nd-card { background: #fff; border: 1px solid var(--border); border-radius: 18px; box-shadow: 0 4px 18px rgba(15,23,42,.06); padding: 22px 20px; margin-bottom: 14px; }
.nd-top { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; }
.nd-logo { width: 44px; height: 44px; border-radius: 12px; overflow: hidden; flex-shrink: 0; background: var(--primary-bg); color: var(--primary); display: flex; align-items: center; justify-content: center; font-weight: 800; }
.nd-logo img { width: 100%; height: 100%; object-fit: cover; }
.nd-source { font-weight: 700; font-size: .9rem; }
.nd-time { font-size: .76rem; color: var(--text-muted); margin-top: 2px; }
.nd-title { font-size: 1.2rem; font-weight: 800; letter-spacing: -.02em; line-height: 1.3; margin-bottom: 10px; }
.nd-msg { font-size: .95rem; color: var(--text-muted); line-height: 1.65; white-space: pre-wrap; }

.nd-item { display: flex; gap: 14px; align-items: center; background: #fff; border: 1px solid var(--border); border-radius: 16px; box-shadow: 0 4px 18px rgba(15,23,42,.06); padding: 14px; margin-bottom: 14px; }
.nd-item .it-thumb { width: 64px; height: 64px; border-radius: 12px; overflow: hidden; flex-shrink: 0; background: var(--primary-bg); display: flex; align-items: center; justify-content: center; color: var(--primary); }
.nd-item .it-thumb img { width: 100%; height: 100%; object-fit: cover; }
.nd-item .it-name { font-weight: 800; font-size: .98rem; }
.nd-item .it-desc { font-size: .8rem; color: var(--text-muted); margin-top: 2px; }
.nd-item .it-price { font-weight: 800; color: var(--primary); font-size: .95rem; margin-top: 4px; }

.nd-cta { display: flex; align-items: center; justify-content: center; gap: 8px; width: 100%; height: 56px; border: none; border-radius: 16px; background: var(--primary); color: #fff; font-family: var(--font); font-weight: 700; font-size: 1rem; text-decoration: none; box-shadow: 0 8px 22px rgba(34,90,227,.3); }
.nd-cta:active { background: var(--primary-dark); }
</style>

<div class="nd-wrap">
  <div class="sub-topbar">
    <a href="notifications.php" class="sub-back" aria-label="Back" onclick="return goBack(event)">
      <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><path d="M19 12H5m0 0l7 7M5 12l7-7"/></svg>
    </a>
    <span class="sub-title">Notification</span>
  </div>

  <div class="nd-card">
    <div class="nd-top">
      <div class="nd-logo">
        <?php if ($has_logo): ?><img src="<?php echo htmlspecialchars($logo_path); ?>" alt=""><?php else: echo htmlspecialchars(strtoupper(substr($source, 0, 1))); endif; ?>
      </div>
      <div>
        <div class="nd-source"><?php echo htmlspecialchars($source); ?></div>
        <div class="nd-time"><?php echo htmlspecialchars($full_date); ?></div>
      </div>
    </div>
    <div class="nd-title"><?php echo htmlspecialchars($n['title']); ?></div>
    <div class="nd-msg"><?php echo htmlspecialchars($n['message']); ?></div>
  </div>

  <?php if ($item): ?>
  <div class="nd-item">
    <div class="it-thumb">
      <?php if ($img): ?><img src="<?php echo htmlspecialchars($img); ?>" alt=""><?php else: ?>
        <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round"><path d="M3 11l19-9-9 19-2-8-8-2z"/></svg>
      <?php endif; ?>
    </div>
    <div style="flex:1;min-width:0">
      <div class="it-name"><?php echo htmlspecialchars($item['name']); ?></div>
      <?php if (!empty($item['description'])): ?><div class="it-desc"><?php echo htmlspecialchars($item['description']); ?></div><?php endif; ?>
      <div class="it-price">₦<?php echo number_format((float)$item['price'], 2); ?></div>
    </div>
  </div>
  <?php endif; ?>

  <?php if ($is_rest): ?>
  <a href="<?php echo htmlspecialchars($deep_link); ?>" class="nd-cta">
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><path d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2"/><rect x="9" y="3" width="6" height="4" rx="1"/><path d="M9 12h6M9 16h4"/></svg>
    <?php echo $item ? 'View dish &amp; order' : 'View menu &amp; order'; ?>
  </a>
  <?php endif; ?>
</div>

<?php include("includes/footer.php"); ?>
