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

if (!isset($_SESSION['user_id'])) {
    header("Location: ../../../index.php");
    exit();
}

$user_id = $_SESSION['user_id'];
$message_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($message_id <= 0) {
    die("Invalid message ID.");
}

// Fetch the message and sender info
$query = "
    SELECT m.*, u.first_name, u.last_name, u.username
    FROM messages m
    JOIN users u ON m.sender_id = u.id
    WHERE m.id = ? AND (m.receiver_id = ? OR m.sender_id = ?)
";
$stmt = $conn->prepare($query);
$stmt->bind_param("iii", $message_id, $user_id, $user_id);
$stmt->execute();
$result = $stmt->get_result();
$message = $result->fetch_assoc();
$stmt->close();

if (!$message) {
    die("Message not found or access denied.");
}

// Mark as read if receiver
if ($message['receiver_id'] == $user_id && !$message['is_read']) {
    $update_stmt = $conn->prepare("UPDATE messages SET is_read = 1 WHERE id = ?");
    $update_stmt->bind_param("i", $message_id);
    $update_stmt->execute();
    $update_stmt->close();
}

// Determine thread ID for navigation
$thread_id = $message['thread_id'] ?? $message['id'];
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Message: <?= htmlspecialchars($message['subject']) ?></title>
    <link rel="stylesheet" href="../../../css/messaging.css" />
    <style>
        body {
            margin: 0;
            font-family: Arial, sans-serif;
            display: flex;
            background-color: #f1f3f4;
            height: 100vh;
        }

        .sidebar {
            width: 240px;
            background-color: #fff;
            padding: 20px;
            box-shadow: 2px 0 5px rgba(0,0,0,0.1);
        }

        .sidebar h3 {
            margin-bottom: 20px;
            color: #202124;
        }

        .sidebar a {
            display: block;
            color: #202124;
            padding: 10px 15px;
            margin-bottom: 8px;
            border-radius: 4px;
            text-decoration: none;
        }

        .sidebar a:hover,
        .sidebar a.active {
            background-color: #e8f0fe;
            color: #1a73e8;
        }

        .main-content {
            flex: 1;
            padding: 30px;
            overflow-y: auto;
        }

        h2 {
            margin-top: 0;
        }

        .message-box {
            background-color: #fff;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 1px 4px rgba(0,0,0,0.08);
        }

        .message-box .header {
            font-size: 0.95em;
            color: #333;
            margin-bottom: 12px;
        }

        .message-box .subject {
            font-size: 1.1em;
            font-weight: bold;
            margin-bottom: 10px;
        }

        .message-box .body {
            margin-bottom: 15px;
            white-space: pre-wrap;
        }

        .attachment {
            margin-bottom: 15px;
        }

        .btn {
            display: inline-block;
            background-color: #1a73e8;
            color: white;
            padding: 10px 16px;
            text-decoration: none;
            border-radius: 4px;
            margin-right: 10px;
        }

        .btn:hover {
            background-color: #155db2;
        }
    </style>
</head>
<body>

<!-- Sidebar -->
<div class="sidebar">
    <h3>Messaging</h3>
    <a href="inbox.php" class="active">Inbox</a>
    <a href="sent.php">Sent</a>
    <a href="compose.php">Compose</a>
</div>

<!-- Main Content -->
<div class="main-content">
    <h2>View Message</h2>

    <div class="message-box">
        <div class="header">
            <strong>From:</strong> <?= htmlspecialchars($message['first_name'] . ' ' . $message['last_name']) ?>
            (<?= htmlspecialchars($message['username']) ?>)<br>
            <span>Sent: <?= date('Y-m-d H:i', strtotime($message['created_at'])) ?></span>
        </div>
        <div class="subject">
            <strong>Subject:</strong> <?= htmlspecialchars($message['subject']) ?>
        </div>
        <div class="body">
            <?= nl2br(htmlspecialchars($message['body'])) ?>
        </div>

        <?php if (!empty($message['attachment'])): ?>
            <div class="attachment">
                <strong>Attachment:</strong>
                <a href="../../../uploads/messages/<?= urlencode($message['attachment']) ?>" target="_blank">
                    <?= htmlspecialchars($message['attachment']) ?>
                </a>
            </div>
        <?php endif; ?>
    </div>

    <br>
    <a class="btn" href="view_thread.php?thread_id=<?= (int)$thread_id ?>">View Full Conversation</a>
    <a class="btn" href="inbox.php">Back to Inbox</a>
</div>

<script>
if (window.refreshMessageBadge) refreshMessageBadge();
</script>

</body>
</html>
