#!/usr/bin/env python3
"""Audit published numerical records; this does not rerun the private engine."""
# SPDX-License-Identifier: MIT
# Copyright (c) 2026 Abyssi.
import hashlib
import json
from pathlib import Path

root = Path(__file__).resolve().parent.parent
for line in (root / 'SHA256SUMS').read_text().splitlines():
    digest, name = line.split('  ', 1)
    assert hashlib.sha256((root / name).read_bytes()).hexdigest() == digest, name
inputs = json.loads((root / 'data/inputs-and-reference.json').read_text())['profiles']
actual = json.loads((root / 'data/abyssi-captures.json').read_text())['captures']
assert len(inputs) == len(actual) == 33
maximum = 0
for expected, observed in zip(inputs, actual):
    assert expected['id'] == observed['id']
    assert expected['expectedStops'] == observed['stops'], expected['id']
    assert expected['expectedStopOxygen'] == observed['stopOxygen']
    assert abs(expected['expectedRuntime'] - observed['runtime']) < 1e-8
    assert observed['finalDepth'] == 0
    assert len(expected['expectedFinalTissues']) == len(observed['finalTissues']) == 16
    error = max(abs(a-b) for x, y in zip(expected['expectedFinalTissues'], observed['finalTissues']) for a, b in zip(x, y))
    assert error < 1e-10, (expected['id'], error)
    maximum = max(maximum, error)
reference = json.loads((root / 'data/dive-deco-native.json').read_text())
abyssi = json.loads((root / 'data/abyssi-second-oracle.json').read_text())
assert reference['coefficients'] == abyssi['coefficients']
assert len(reference['cases']) == len(abyssi['cases']) == 4
for expected, observed in zip(reference['cases'], abyssi['cases']):
    assert expected['name'] == observed['name']
    error = max(abs(a-b) for x, y in zip(expected['bottomTissues'], observed['matchedPressureBottomTissues']) for a, b in zip(x, y))
    assert error < 1e-10
print(f'Published records verified: 33 schedule entries; maximum final-tissue difference {maximum:.6e} bar.')
print('Second-reference coefficients and 4 matched-pressure bottom-tissue records verified.')
print('Three decompression schedules remain unequal; negative findings are retained in the dataset.')
print('This is a data-integrity audit, not independent execution of Abyssi or a safety validation.')
