#!/usr/bin/env python3
"""Recheck sanitized original-table evidence; never requires secrets or raw rows."""
import json
from pathlib import Path

BASE = Path(__file__).resolve().parent

def read(name):
    return json.loads((BASE / name).read_text())

red = {lane: read(f'read-red-{lane}.json') for lane in 'BC'}
green = {lane: read(f'read-green-{lane}.json') for lane in 'BC'}
logs = {lane: read(f'logs-red-{lane}.json') for lane in 'BC'}
checks = {}
for lane, expected in [('B', 17), ('C', 15)]:
    g = green[lane]
    checks[f'{lane}_actual_HTTP_count'] = g['http_requests'] == expected
    checks[f'{lane}_CLI_assertions_green'] = g['cli_stats']['assertions']['failed'] == 0
    checks[f'{lane}_all_original_values_and_shapes'] = all(c['source_equal'] for c in g['cases'])
    checks[f'{lane}_six_real_GET'] = len([c for c in g['cases'] if c['method'] == 'GET']) == 6
    checks[f'{lane}_two_GET_common_rejection'] = len([c for c in g['cases'] if c['method'] == 'GET' and c['classification'] == 'common_rejection']) == 2
    checks[f'{lane}_original_tables_unchanged'] = all(t['unchanged'] and t['metadata_unchanged'] for t in g['independent_audit'].values())
    checks[f'{lane}_three_native_log_columns_or_more'] = next(c for c in g['cases'] if c['label'] == 'post-logs-natural')['rows'] > 0
checks['C_six_actual_formatter_reds'] = len([c for c in red['C']['cases'] if c['classification'] == 'FAIL']) == 6
checks['C_real_log_precision_red'] = logs['C']['cases'][0]['differences'] == [{'path': 'data.0.create_time', 'kind': 'value', 'actual_type': 'string', 'expected_type': 'string'}]
checks['B_complete_wire_unchanged'] = {c['label']: c['raw_wire_sha256'] for c in red['B']['cases']} == {c['label']: c['raw_wire_sha256'] for c in green['B']['cases']}
checks['unique_method_paths_10'] = len(set(green['B']['unique_method_paths']) | set(green['C']['unique_method_paths'])) == 10
print(json.dumps({'checks': checks, 'passed': all(checks.values())}, ensure_ascii=False, indent=2))
raise SystemExit(0 if all(checks.values()) else 1)
