<?php
session_start();
require 'db_connection.php';

if (!isset($_GET['id'])) {
    die("Invalid request");
}

$id = $_GET['id'];
$user_role = $_SESSION['role'];

/* fetch full notification */
$stmt = $conn->prepare("
SELECT n.message_head, n.message_body, n.created_at
FROM notifications n
JOIN notification_recipients r ON n.id = r.notification_id
WHERE n.id = ? AND (r.role = ? OR r.role = 'general')
");

$stmt->bind_param("is", $id, $user_role);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows == 0) {
    die("Notification not found");
}

$data = $result->fetch_assoc();

/* mark as read */
$update = $conn->prepare("
UPDATE notification_recipients
SET status='read'
WHERE notification_id=? AND role=?
");
$update->bind_param("is", $id, $user_role);
$update->execute();
?>

<h2><?= htmlspecialchars($data['message_head']) ?></h2>
<hr>
<p><?= nl2br(htmlspecialchars($data['message_body'])) ?></p>
<small><?= $data['created_at'] ?></small>