<?php
session_start();
if (!isset($_SESSION['student_id'])) {
    die("Student not logged in.");
}

$studentId = $_SESSION['student_id'];

if (isset($_GET['sender_id']) && isset($_GET['message_id'])) {
    $senderId = $_GET['sender_id'];
    $messageId = $_GET['message_id'];
} else {
    die("Invalid message or sender.");
}

include('db_connection.php'); // Ensure this path is correct

// Fetch sender's name
$query = "SELECT name FROM profiles WHERE student_id = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param('i', $senderId);
$stmt->execute();
$result = $stmt->get_result();
$sender = $result->fetch_assoc();
$senderName = $sender['name'];

// Fetch parent message
$parentQuery = "SELECT message FROM student_messages WHERE id = ?";
$parentStmt = $conn->prepare($parentQuery);
$parentStmt->bind_param('i', $messageId);
$parentStmt->execute();
$parentResult = $parentStmt->get_result();
$parentMessage = $parentResult->fetch_assoc();

// Update message status to 'Read'
$updateQuery = "UPDATE student_messages SET status = 'Read' WHERE id = ? AND receiver_id = ?";
$updateStmt = $conn->prepare($updateQuery);
$updateStmt->bind_param('ii', $messageId, $studentId);
$updateStmt->execute();

// Handle reply submission
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $replyMessage = $_POST['reply_message'];
    $timestamp = date('Y-m-d H:i:s');

    $insertQuery = "INSERT INTO student_messages (sender_id, receiver_id, message, timestamp, status, parent_message_id) VALUES (?, ?, ?, ?, 'Unread', ?)";
    $insertStmt = $conn->prepare($insertQuery);
    $insertStmt->bind_param('iissi', $studentId, $senderId, $replyMessage, $timestamp, $messageId);
    $insertStmt->execute();

    echo "Reply sent successfully.";
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Reply to Message</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #18191a;
            color: #e4e6eb;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .reply-container {
            background-color: #242526;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
            width: 300px;
            text-align: center;
        }

        .reply-container h2 {
            margin: 0 0 20px 0;
        }

        .reply-container .parent-message {
            margin-bottom: 20px;
            padding: 10px;
            background-color: #3a3b3c;
            border-radius: 5px;
        }

        .reply-container textarea {
            width: 100%;
            height: 100px;
            padding: 10px;
            border-radius: 5px;
            border: 1px solid #3a3b3c;
            margin-bottom: 10px;
            background-color: #18191a;
            color: #e4e6eb;
        }

        .reply-container button {
            background-color: #2e89ff;
            color: #fff;
            padding: 10px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="reply-container">
        <h2>Reply to <?php echo htmlspecialchars($senderName); ?></h2>
        <div class="parent-message">
            <p><strong>Reply To:</strong></p>
            <p><?php echo htmlspecialchars($parentMessage['message']); ?></p>
        </div>
        <form method="post" action="">
            <textarea name="reply_message" placeholder="Type your reply here..." required></textarea><br>
            <button type="submit">Send Reply</button>
        </form>
    </div>
</body>
</html>
