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

check_role('alumno');

$user = get_logged_in_user();

if (!isset($_GET['id'])) {
    die('ID de notificación no proporcionado');
}

$notification_id = (int)$_GET['id'];

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

if (!$notification) {
    die('Notificación no encontrada');
}

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

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

<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h5 class="card-title mb-0">
                    <span class="badge <?php echo $badge_class; ?> me-2"><?php echo ucfirst($notification['type']); ?></span>
                    <?php echo htmlspecialchars($notification['title'] ?: 'Sin título'); ?>
                </h5>
            </div>
            <div class="card-body">
                <div class="row mb-3">
                    <div class="col-12">
                        <strong>Estado:</strong> <?php echo $notification['is_read'] ? '<span class="badge badge-secondary">Leída</span>' : '<span class="badge badge-primary">No leída</span>'; ?>
                    </div>
                </div>

                <div class="row mb-3">
                    <div class="col-12">
                        <strong>Fecha de creación:</strong><br>
                        <?php echo date('l, F d, Y \a \l\a\s H:i', strtotime($notification['created_at'])); ?>
                    </div>
                </div>

                <div class="row">
                    <div class="col-12">
                        <strong>Mensaje:</strong><br>
                        <div class="alert alert-light border mt-3">
                            <p class="mb-0"><?php echo nl2br(htmlspecialchars($notification['message'])); ?></p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>