<?php
define("IN_SITE", true);
require_once("../../core/DB.php");      
require_once("../../core/helpers.php"); 
if (isset($_GET['outgoing_msg_id'], $_GET['incoming_msg_id'])) {

    $outgoing_msg_id = (int)$_GET['outgoing_msg_id'];
    $incoming_msg_id = (int)$_GET['incoming_msg_id'];

    // Prepare the SQL query
    $sql = "SELECT * FROM messages 
            WHERE (incoming_msg_id = $incoming_msg_id AND outgoing_msg_id = $outgoing_msg_id)
               OR (incoming_msg_id = $outgoing_msg_id AND outgoing_msg_id = $incoming_msg_id) 
            ORDER BY msg_id DESC ";
    
    $result = $DVR->query($sql);
    
    $messages = [];
    while ($row = $result->fetch_assoc()) {
        $messages[] = $row;
    }
    
    echo json_encode($messages);
} else {
    echo json_encode(['status' => 'error', 'message' => 'Invalid request']);
}
?>