<?php
declare(strict_types=1);
// Offline verification only. Never creates a connection or replays a statement.
$file = $argv[1] ?? '/Users/wiscourper/Documents/workspace/local/dm8-batch116-20261012.S1TtvG/dishes199-v3-events.jsonl';
$raw = file_get_contents($file);
$checks = 0;
$need = static function (bool $ok, string $reason) use (&$checks): void {
    $checks++;
    if (!$ok) throw new RuntimeException($reason);
};
$need(hash('sha256', $raw) === '3d6ae9b84174dbc9330173e2fda5cf7023dac8fd50ce83239fbf360522bc46a2', 'Actual sealed journal');
$events = array_map(static function ($line) { return json_decode($line, true, 512, JSON_THROW_ON_ERROR); }, explode("\n", trim($raw)));
$need(count($events) === 276, 'Full event count');
$snapshots = []; $mutations = []; $cas = []; $tx = []; $reads = 0;
foreach ($events as $i => $e) {
    $need($e['seq'] === $i + 1, 'Contiguous durable sequence');
    $need(!in_array($e['event'], ['stopped', 'native-error'], true), 'No hidden stop/error');
    $d = $e['data'];
    if ($e['event'] === 'intent') {
        if ($d['kind'] === 'read') { $reads++; $need(strpos($d['SQL'], 'SELECT ') === 0, 'Actual read statement'); }
        else $mutations[] = $d;
    }
    if ($e['event'] === 'native-result') {
        $need($d['success'] === true && $d['SQLSTATE'] === '00000' && $d['parent_execute_calls'] === 1, 'Native success');
        if ($d['kind'] === 'CAS') $need($d['rowCount'] === 1, 'Exactly one CAS row');
    }
    if ($e['event'] === 'snapshot') $snapshots[] = $d;
    if ($e['event'] === 'CAS-readback') $cas[] = $d;
    if ($e['event'] === 'txn-result') $tx[] = $d;
}
$need($reads === 60 && count($mutations) === 5, 'Actual SQL budget');
$need(array_column($mutations, 'key') === ['create-trigger', 'CAS-10628', 'CAS-10629', 'CAS-10631', 'alter-two-dates'], 'Exact mutation order');
$need($mutations[0]['SQL_SHA'] === 'f63b3d4e450194a41ad2ce5a889e20fc154784268b0d220de8c73d4bed142b0e', 'Exact native trigger SQL');
$need($mutations[4]['SQL_SHA'] === 'c58acb674a3c1d14d94b21f0873c791b957bed1598b5be86e9de289d0769ad33', 'Exact combined ALTER');
$old = [10628 => '2026-10-10 18:49:12.169449', 10629 => '2026-10-11 06:47:28.533709', 10631 => '2026-10-12 19:48:30.295610'];
$need(count($cas) === 3, 'Three full-row CAS readbacks');
foreach ($cas as $i => $c) {
    $id = $c['id'];
    $need(isset($old[$id]) && $c['old_FF6'] === $old[$id] && $c['new_floor'] === substr($old[$id], 0, 19), 'Exact floor, not rounding');
    $need($c['full84_verified'] === true && strlen($c['unchanged83_typed_MAC']) === 64, 'Other83 CAS columns protected');
    $need($mutations[$i + 1]['SQL_SHA'] === 'e928dc02390db9bf054108f98057cedab964345816236ef5ae7f9162bcd87301'
        && $mutations[$i + 1]['binds'] === [substr($old[$id], 0, 19), $id, $old[$id]], 'Exact bound CAS values');
}
$need(array_column($tx, 'method') === ['beginTransaction', 'commit'], 'One explicit transaction');
$need($tx[0]['success'] === true && $tx[0]['in_transaction'] === true
    && $tx[1]['success'] === true && $tx[1]['in_transaction'] === false, 'Known commit completed');
$need(array_column($snapshots, 'phase') === ['baseline', 'after-trigger', 'after-CAS-commit', 'after-ALTER', 'fresh-final'], 'Five full stages');
$base = $snapshots[0];
foreach ($snapshots as $i => $s) {
    $need($s['count'] === 5489 && $s['other82_typed_MAC'] === $base['other82_typed_MAC'] && $s['PK_MAC'] === $base['PK_MAC'], 'Original row set/other82/PK');
    foreach (['indexes', 'index_columns', 'constraints', 'constraint_columns'] as $key) $need($s['metadata'][$key] === $base['metadata'][$key], 'Original structure ' . $key);
    $cols = $s['metadata']['columns'];
    $need(count($cols) === 84, 'All columns');
    foreach ($cols as $j => $col) {
        $expected = $base['metadata']['columns'][$j];
        if ($i >= 3 && in_array($col['column_name'], ['CREATE_TIME', 'UPDATE_TIME'], true)) {
            $expected['data_scale'] = 0.0;
            $expected['data_default'] = 'CURRENT_TIMESTAMP(0)';
        }
        $need($col === $expected, 'Strict typed definition, including CREATE_TIMES');
    }
    if ($i > 0) {
        $need(count($s['metadata']['triggers']) === 1 && $s['metadata']['triggers'][0]['status'] === 'Y'
            && $s['metadata']['objects'][0]['status'] === 'VALID', 'Compiled enabled trigger');
    }
}
$need($snapshots[0]['two_dates_typed_MAC'] === $snapshots[1]['two_dates_typed_MAC'], 'CREATE did not modify dates');
$need($snapshots[2]['two_dates_typed_MAC'] === $snapshots[3]['two_dates_typed_MAC']
    && $snapshots[3]['two_dates_typed_MAC'] === $snapshots[4]['two_dates_typed_MAC'], 'Calibrated full date map stable after ALTER and new PDO');
$last = end($events);
$need($last['event'] === 'complete' && $last['data']['DDL'] === 2 && $last['data']['UPDATE_affected'] === 3
    && $last['data']['transactions_committed'] === 1 && $last['data']['HTTP'] === 0, 'Final boundary');
echo json_encode(['verified' => true, 'checks' => $checks, 'events' => 276, 'SELECT' => 60,
    'earlier_readonly_attempt_SELECT' => 22, 'cumulative_SELECT' => 82, 'DDL' => 2,
    'calibrated_rows' => 3, 'preserved_other_columns' => 82, 'rows' => 5489,
    'fresh_connection_verified' => true, 'business_HTTP_in_this_migration' => 0]), PHP_EOL;
