<?php
include 'db_connection.php';

$message_id = isset($_POST['message_id']) ? intval($_POST['message_id']) : 0;
$reply = isset($_POST['reply']) ? $_POST['reply'] : '';

if ($message_id <= 0 || empty($reply)) {
    echo json_encode(['status' => 'error', 'message' => 'Invalid request']);
    exit();
}

// Insert new reply
$sql = "INSERT INTO message_replies (message_id, sender_id, reply, timestamp) VALUES (?, ?, ?, NOW())";
$stmt = $conn->prepare($sql);
$sender_id = 1; // Replace with actual sender_id logic
$stmt->bind_param("iis", $message_id, $sender_id, $reply);
$result = $stmt->execute();

if ($result) {
    echo json_encode(['status' => 'success', 'message' => 'Reply sent successfully']);
} else {
    echo json_encode(['status' => 'error', 'message' => 'Failed to send reply']);
}

$stmt->close();
$conn->close();
?>
