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

$studentId = $_SESSION['student_id'];
$recipientId = isset($_GET['student_id']) ? intval($_GET['student_id']) : 0;

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

function getMessages($studentId, $recipientId, $conn) {
    $query = "SELECT sm.id, sm.sender_id, sm.receiver_id, sm.message, sm.timestamp, sm.status, p.name AS sender_name
              FROM student_messages sm
              JOIN profiles p ON sm.sender_id = p.student_id
              WHERE (sm.sender_id = ? AND sm.receiver_id = ?) OR (sm.sender_id = ? AND sm.receiver_id = ?)
              ORDER BY sm.timestamp DESC";

    $stmt = $conn->prepare($query);
    $stmt->bind_param('iiii', $studentId, $recipientId, $recipientId, $studentId);
    $stmt->execute();
    $result = $stmt->get_result();

    $messages = [];
    while ($row = $result->fetch_assoc()) {
        $messages[] = $row;
    }
    return $messages;
}

$messages = getMessages($studentId, $recipientId, $conn);
$recipientQuery = "SELECT name, profile_picture FROM profiles WHERE student_id = ?";
$recipientStmt = $conn->prepare($recipientQuery);
$recipientStmt->bind_param('i', $recipientId);
$recipientStmt->execute();
$recipientResult = $recipientStmt->get_result();
$recipient = $recipientResult->fetch_assoc();

$conn->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Chat with <?php echo htmlspecialchars($recipient['name']); ?></title>
    <link rel="stylesheet" href="styles.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
      body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            color: #333;
            margin: 0;
            padding: 0;
        }
        header {
            background: #007bff;
            color: white;
            padding: 10px 20px;
            text-align: center;
        }
        .chat-container {
            max-width: 800px;
            margin: 20px auto;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
            overflow: hidden;
        }
        .chat-header {
            background: #007bff;
            color: white;
            padding: 15px;
            display: flex;
            align-items: center;
            border-bottom: 1px solid #ddd;
        }
        .chat-header img {
            border-radius: 50%;
            width: 50px;
            height: 50px;
            margin-right: 15px;
        }
        .chat-header h1 {
            margin: 0;
            font-size: 18px;
        }
        .chat-messages {
            height: 500px;
            overflow-y: auto;
            padding: 20px;
            border-bottom: 1px solid #ddd;
            background: #fafafa;
        }
        .message {
            margin-bottom: 15px;
        }
        .message p {
            margin: 0;
            padding: 5px;
            border-radius: 8px;
            background: #f1f1f1;
        }
        .message .sender-name {
            font-weight: bold;
            margin-bottom: 5px;
        }
        .message .message-time {
            font-size: 0.8em;
            color: #888;
        }
        .message-form {
            padding: 15px;
        }
        .message-form textarea {
            width: calc(100% - 22px);
            height: 60px;
            padding: 10px;
            border-radius: 8px;
            border: 1px solid #ddd;
            margin-bottom: 10px;
        }
        .message-form button {
            background: #007bff;
            color: white;
            border: none;
            border-radius: 8px;
            padding: 10px 20px;
            cursor: pointer;
            font-size: 16px;
        }
        .message-form button:hover {
            background: #0056b3;
        }
    </style>
</head>
<body>
    <header>
        <a href="social_dashboard.php">Back</a>
        <h1>Chat with <?php echo htmlspecialchars($recipient['name']); ?></h1>
    </header>
    <div class="chat-container">
        <div class="chat-messages">
            <?php foreach ($messages as $message): ?>
            <div class="message">
                <div class="message-details">
                    <p><strong><?php echo htmlspecialchars($message['sender_name']); ?>:</strong> <?php echo htmlspecialchars($message['message']); ?></p>
                    <p class="message-time"><?php echo date('Y-m-d H:i', strtotime($message['timestamp'])); ?></p>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
        <form action="send_message.php" method="post" class="message-form">
            <input type="hidden" name="recipient_id" value="<?php echo $recipientId; ?>">
            <textarea name="message" placeholder="Type your message here..." required></textarea>
            <button type="submit">Send</button>
        </form>
    </div>
</body>
</html>
