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

echo '<style>
.notification-badge {
    font-size: 0.75rem;
    font-weight: 600;
    padding: 0.375rem 0.75rem;
    border-radius: 20px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    border: 2px solid;
    display: inline-block;
    min-width: 80px;
    text-align: center;
}

.notification-badge-info {
    background: linear-gradient(135deg, #17a2b8, #20c997);
    color: white;
    border-color: #17a2b8;
    box-shadow: 0 2px 4px rgba(23, 162, 184, 0.3);
}

.notification-badge-success {
    background: linear-gradient(135deg, #28a745, #20c997);
    color: white;
    border-color: #28a745;
    box-shadow: 0 2px 4px rgba(40, 167, 69, 0.3);
}

.notification-badge-warning {
    background: linear-gradient(135deg, #ffc107, #fd7e14);
    color: #212529;
    border-color: #ffc107;
    box-shadow: 0 2px 4px rgba(255, 193, 7, 0.3);
}

.notification-badge-danger {
    background: linear-gradient(135deg, #dc3545, #fd7e14);
    color: white;
    border-color: #dc3545;
    box-shadow: 0 2px 4px rgba(220, 53, 69, 0.3);
}

.status-badge {
    font-size: 0.7rem;
    font-weight: 500;
    padding: 0.25rem 0.5rem;
    border-radius: 12px;
    text-transform: uppercase;
    letter-spacing: 0.3px;
}

.status-badge-primary {
    background: linear-gradient(135deg, #007bff, #6610f2);
    color: white;
}

.status-badge-secondary {
    background: linear-gradient(135deg, #6c757d, #495057);
    color: white;
}
</style>';

check_role('administrador');

if (!isset($_GET['id'])) {
    die('Notification ID not provided');
}

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

$stmt = $pdo->prepare("SELECT n.*, u.first_name, u.last_name, u.email, r.name as role_name
                      FROM notifications n
                      JOIN users u ON n.user_id = u.id
                      JOIN roles r ON u.role_id = r.id
                      WHERE n.id = ?");
$stmt->execute([$notification_id]);
$notification = $stmt->fetch();

if (!$notification) {
    die('Notification not found');
}

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

$badge_class = '';
switch ($notification['type']) {
    case 'info':
        $badge_class = 'notification-badge notification-badge-info';
        break;
    case 'success':
        $badge_class = 'notification-badge notification-badge-success';
        break;
    case 'warning':
        $badge_class = 'notification-badge notification-badge-warning';
        break;
    case 'danger':
        $badge_class = 'notification-badge notification-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-sm-6">
                        <strong>Usuario:</strong><br>
                        <?php echo htmlspecialchars($notification['first_name'] . ' ' . $notification['last_name']); ?><br>
                        <small class="text-muted"><?php echo htmlspecialchars($notification['email']); ?></small>
                    </div>
                    <div class="col-sm-6">
                        <strong>Rol:</strong><br>
                        <?php echo ucfirst($notification['role_name']); ?><br>
                        <strong>Estado:</strong> <?php echo $notification['is_read'] ? '<span class="status-badge status-badge-secondary">Leída</span>' : '<span class="status-badge status-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('d/m/Y \a \l\a\s H:i:s', strtotime($notification['created_at'])); ?>
                    </div>
                </div>

                <div class="row">
                    <div class="col-12">
                        <strong>Mensaje:</strong><br>
                        <div class="alert alert-light border">
                            <?php echo nl2br(htmlspecialchars($notification['message'])); ?>
                        </div>
                    </div>
                </div>
            </div>
            <div class="card-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Cerrar</button>
            </div>
        </div>
    </div>
</div>