<?php
header('Content-Type: application/json');

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $data = json_decode(file_get_contents('php://input'), true);

    if (isset($data['machine_id'])) {
        require './db.php';

        $machine_id = $conn->real_escape_string($data['machine_id']);
        $query = "SELECT command FROM machine WHERE serialNumber = '$machine_id'";
        $result = $conn->query($query);

        if ($result->num_rows > 0) {
            $row = $result->fetch_assoc();
            echo json_encode(['status' => $row['command']]);
        } else {
            echo json_encode(['status' => 'ERROR']);
        }
    } else {
        echo json_encode(['status' => 'INVALID_REQUEST']);
    }
}
?>
