<?php
define("IN_SITE", true);
require_once("../../core/DB.php");
require_once("../../core/helpers.php");
require_once("../../core/is_user.php");

if (isset($_POST['msg'], $_POST['incoming_msg_id'], $_POST['outgoing_msg_id'])) {
   $incoming_msg_id = $_POST['incoming_msg_id'];
    $outgoing_msg_id = (int)$_POST['outgoing_msg_id'];
    $msg = xss(trim($_POST['msg']));
  $date = time();
    if (!empty($msg)) {
     
        $data = [
            'incoming_msg_id' => $incoming_msg_id,
            'outgoing_msg_id' => $outgoing_msg_id,
            'date' => $date,
            'msg' => $msg
        ];
        
   
        $result = $DVR->insert('messages', $data);
        
        if ($result) {
            echo json_encode(['status' => 'success']);
        } else {
            echo json_encode(['status' => 'error', 'message' => 'Database insert failed']);
        }
    } else {
        echo json_encode(['status' => 'error', 'message' => 'Message cannot be empty']);
    }
} else {
    echo json_encode(['status' => 'error', 'message' => 'Invalid request']);
}
?>