<?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 = $_GET['id'] ?? 0;

if (!$notification_id) {
    echo 'Invalid notification ID.';
    exit;
}

// Get notification
$stmt = $pdo->prepare("SELECT n.*, c.name as career_name FROM notifications n LEFT JOIN careers c ON n.career_id = c.id WHERE n.id = ? AND n.user_id = ?");
$stmt->execute([$notification_id, $user['id']]);
$notification = $stmt->fetch();

if (!$notification) {
    echo 'Notification not found.';
    exit;
}

// Mark as read if not already
if (!$notification['is_read']) {
    $stmt = $pdo->prepare("UPDATE notifications SET is_read = 1 WHERE id = ? AND user_id = ?");
    $stmt->execute([$notification_id, $user['id']]);
}

$badge_class = '';
switch ($notification['type']) {
    case 'info':
        $badge_class = 'badge bg-info';
        break;
    case 'success':
        $badge_class = 'badge bg-success';
        break;
    case 'warning':
        $badge_class = 'badge bg-warning';
        break;
    case 'danger':
        $badge_class = 'badge bg-danger';
        break;
}

echo '<div class="notification-detail">
    <div class="mb-3">
        <span class="badge ' . $badge_class . '">' . ucfirst($notification['type']) . '</span>
        <small class="text-muted ml-2">Carrera: ' . htmlspecialchars($notification['career_name'] ?: 'General') . '</small>
    </div>
    <h5>' . htmlspecialchars($notification['title'] ?: 'Sin título') . '</h5>
    <p>' . nl2br(htmlspecialchars($notification['message'])) . '</p>
    <small class="text-muted">Fecha: ' . date('M d, Y H:i', strtotime($notification['created_at'])) . '</small>
</div>';
?>