<?php
declare(strict_types=1);
require __DIR__ . '/compare_batch.php';

$ok = static function (int $status, string $sha, array $shape, int $source): array {
    return ['state' => $status === 200 ? 'business_shape_observed' : 'business_red',
        'business_status' => $status, 'data_sha256' => $sha,
        'response_shape' => $shape, 'source' => ['state' => 'source_read',
            'rows_sampled' => $source]];
};
$a = ['GET /api/one' => $ok(200, 'same', ['money' => 'string'], 1),
    'GET /api/two' => $ok(200, 'empty', ['list' => []], 0)];
$b = $a;
$c = ['GET /api/one' => $ok(200, 'different-snapshot', ['money' => 'string'], 1),
    'GET /api/two' => $ok(200, 'not-empty', ['list' => ['[]' => 'integer']], 1)];
$result = compare_batch_results($a, $b, $c);
if ($result['ab_exact'] !== 2 || $result['c_type_match'] !== 1
    || $result['c_snapshot_branch_unmatched'] !== 1) {
    throw new RuntimeException('Batch comparator overclaimed snapshot parity');
}
$c['GET /api/one']['response_shape']['money'] = 'double';
$result = compare_batch_results($a, $b, $c);
if ($result['c_type_mismatch'] !== 1) {
    throw new RuntimeException('DM JSON scalar type mismatch must stay visible');
}
$a['GET /api/three'] = $ok(200, 'a-empty', ['list' => []], 1);
$a['GET /api/three']['branch'] = 'empty_only';
$b['GET /api/three'] = $a['GET /api/three'];
$c['GET /api/three'] = $ok(200, 'c-nonempty',
    ['list' => ['[]' => ['id' => 'integer']]], 1);
$c['GET /api/three']['branch'] = 'nonempty_or_scalar_candidate';
$result = compare_batch_results($a, $b, $c);
if ($result['c_snapshot_branch_unmatched'] !== 2) {
    throw new RuntimeException('Same source presence does not mean same response branch');
}
$blocked = ['state' => 'source_blocked',
    'source' => ['state' => 'source_actor_staff_absent']];
$a['POST /api/four'] = $blocked;
$b['POST /api/four'] = $blocked;
$c['POST /api/four'] = $blocked;
$result = compare_batch_results($a, $b, $c);
if (($result['not_requested'] ?? 0) !== 1 || $result['ab_same_error'] !== 0) {
    throw new RuntimeException('Three skipped routes are not shared business errors');
}
echo "batch comparison tests: 4 green\n";
