<?php
require_once __DIR__ . '/helpers.php';

$payload = delivery_json_input();
$tenant = delivery_load_tenant();
$manifest = delivery_manifest($payload);

$reports = array_values(array_filter(array_map('delivery_report_normalise', $payload['reports'] ?? [])));
$groups = [];

foreach ($reports as $report) {
    $key = (int)$report['surveyor'];
    if ($key < 1) {
        $key = 0;
    }
    $groups[$key][] = $report;
}

ksort($groups, SORT_NUMERIC);

delivery_response([
    'ok' => true,
    'groups' => array_values($groups),
]);
