<?php
if(file_exists('../../../'.$_REQUEST['tenant'].'/config.php')) {
    require_once('../../../'.$_REQUEST['tenant'].'/config.php');
}

// Get POST data
$recipient_id='0';
$assignment_id = $_POST['assignment_id'];
$message = $_POST['message'];
$status = $_POST['status'];  // 'normal' or 'urgent'
$sender_type = 'surveyor';  // Surveyor is sending the message

// Insert the message into the messages table
$sql = "INSERT INTO messages (assignment_id, sender_type, recipient_id, message, status, sent_at) 
        VALUES (?, ?, ?, ?, ?, UNIX_TIMESTAMP())";
$stmt = $con->prepare($sql);
$stmt->bind_param('isiss', $assignment_id, $sender_type, $recipient_id, $message, $status);

if ($stmt->execute()) {
    echo 'success';
} else {
    echo 'error';
}

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