<?php
// File: get_engineers.php
// Purpose: Provide a list of engineers for the autocomplete feature

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

// URL of the users.json file
$users_json_url = 'https://rxaero.unaux.com/data/users.json';

// Fetch the remote JSON file
$context = stream_context_create(['http' => ['timeout' => 5]]);
$json_data = file_get_contents($users_json_url, false, $context);

if ($json_data === false) {
    echo json_encode(['error' => 'Unable to fetch engineer list.']);
    exit;
}

$users = json_decode($json_data, true);
$engineers = [];

if (is_array($users)) {
    foreach ($users as $user) {
        // Include user if they have a full_name and are either employee or admin
        if (!empty($user['full_name']) && in_array($user['role'] ?? '', ['employee', 'admin'])) {
            $engineers[] = [
                'id' => $user['id'],
                'full_name' => $user['full_name'],
                'amo_stamp_number' => $user['amo_stamp_number'] ?? ''
            ];
        }
    }
}

echo json_encode($engineers);
?>