<?php
// /admin/view_thread.php
$page_title = "View Conversation";
require_once 'includes/header.php';

// 1. Get and validate the IDs from the URL
$ad_id = filter_input(INPUT_GET, 'ad_id', FILTER_VALIDATE_INT);
$sender_id = filter_input(INPUT_GET, 'sender_id', FILTER_VALIDATE_INT);
$receiver_id = filter_input(INPUT_GET, 'receiver_id', FILTER_VALIDATE_INT);

if (!$ad_id || !$sender_id || !$receiver_id) {
    die("Error: Invalid conversation specified. Please go back and try again.");
}

// 2. Fetch general info about the thread (ad title, participants) for the page header
$info_stmt = $conn->prepare("
    SELECT a.title, s.username as sender_name, r.username as receiver_name
    FROM ads a, users s, users r
    WHERE a.id = ? AND s.id = ? AND r.id = ?
");
$info_stmt->bind_param("iii", $ad_id, $sender_id, $receiver_id);
$info_stmt->execute();
$thread_info = $info_stmt->get_result()->fetch_assoc();
$info_stmt->close();

if (!$thread_info) {
    die("Error: Could not find conversation details.");
}

// 3. Fetch all messages in this thread (both ways)
$messages_stmt = $conn->prepare("
    SELECT m.message, m.created_at, sender.username as sender_username
    FROM messages m
    JOIN users sender ON m.sender_id = sender.id
    WHERE m.ad_id = ? 
      AND ((m.sender_id = ? AND m.receiver_id = ?) OR (m.sender_id = ? AND m.receiver_id = ?))
    ORDER BY m.created_at ASC
");
$messages_stmt->bind_param("iiiii", $ad_id, $sender_id, $receiver_id, $receiver_id, $sender_id);
$messages_stmt->execute();
$messages_result = $messages_stmt->get_result();
$messages = $messages_result->fetch_all(MYSQLI_ASSOC);
$messages_stmt->close();
?>

<div class="bg-white p-6 rounded-lg shadow-md">
    <div class="flex justify-between items-center mb-4">
        <div>
            <h1 class="text-2xl font-bold">Conversation</h1>
            <p class="text-gray-600">
                Regarding Ad: <strong><?php echo escape($thread_info['title']); ?></strong><br>
                Between: <strong><?php echo escape($thread_info['sender_name']); ?></strong> and <strong><?php echo escape($thread_info['receiver_name']); ?></strong>
            </p>
        </div>
        <a href="manage_messages.php" class="bg-gray-500 hover:bg-gray-600 text-white font-bold py-2 px-4 rounded">
            &larr; Back to Threads
        </a>
    </div>

    <hr class="my-4">

    <div class="space-y-4">
        <?php if (empty($messages)): ?>
            <p class="text-center text-gray-500 py-10">No messages found for this thread.</p>
        <?php else: ?>
            <?php foreach ($messages as $message): ?>
                <div class="p-4 rounded-lg <?php echo ($message['sender_username'] === $thread_info['sender_name']) ? 'bg-blue-50' : 'bg-gray-50'; ?>">
                    <div class="flex justify-between items-center mb-1">
                        <p class="font-bold text-sm text-gray-800"><?php echo escape($message['sender_username']); ?></p>
                        <p class="text-xs text-gray-500"><?php echo date('M d, Y H:i', strtotime($message['created_at'])); ?></p>
                    </div>
                    <p class="text-gray-700">
                        <?php echo nl2br(escape($message['message'])); ?>
                    </p>
                </div>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>
</div>

<?php
require_once 'includes/footer.php';
?>