<?php
session_start();
include('../../../includes/config.php');

$user_id = $_SESSION['user_id'];
$messages = $conn->query("SELECT m.*, CONCAT(u.first_name, ' ', u.last_name) AS sender_name 
                          FROM messages m 
                          JOIN users u ON m.sender_id = u.id 
                          WHERE receiver_id = $user_id 
                          ORDER BY sent_at DESC");
?>

<h2>Inbox</h2>
<?php while ($msg = $messages->fetch_assoc()): ?>
    <div>
        <a href="view.php?id=<?= $msg['id'] ?>">
            <strong><?= htmlspecialchars($msg['subject']) ?></strong> 
            from <?= htmlspecialchars($msg['sender_name']) ?> 
            (<?= $msg['status'] ?>)
        </a>
    </div>
<?php endwhile; ?>
