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

$sql = "SELECT assignment_id, message, status, sent_at 
        FROM messages 
        WHERE sender_type = 'surveyor' 
        AND is_read = 0 
        ORDER BY sent_at DESC";

$result = $con->query($sql);
$messages = [];

while ($row = $result->fetch_assoc()) {
    $messages[] = [
        'assignment_id' => $row['assignment_id'],
        'message' => $row['message'],
        'status' => $row['status'],
        'sent_at' => date('d-m-Y H:i', $row['sent_at'])
    ];
}

echo json_encode(['messages' => $messages]);
$con->close();
?>