<?php
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}
require_once '../includes/db.php';
require_once '../includes/auth.php';

check_role('egresado');

$user = get_logged_in_user();

$notification_id = $_POST['notification_id'] ?? 0;

if (!$notification_id) {
    echo json_encode(['success' => false, 'message' => 'Invalid notification ID.']);
    exit;
}

// Check if notification belongs to user
$stmt = $pdo->prepare("SELECT id FROM notifications WHERE id = ? AND user_id = ?");
$stmt->execute([$notification_id, $user['id']]);
$notification = $stmt->fetch();

if (!$notification) {
    echo json_encode(['success' => false, 'message' => 'Notification not found.']);
    exit;
}

// Mark as read
$stmt = $pdo->prepare("UPDATE notifications SET is_read = 1 WHERE id = ? AND user_id = ?");
if ($stmt->execute([$notification_id, $user['id']])) {
    echo json_encode(['success' => true]);
} else {
    echo json_encode(['success' => false, 'message' => 'Failed to mark notification as read.']);
}
?>