#!/usr/bin/env python3
"""Restore historical evidence without re-executing any business requests."""
import hashlib
import json
import subprocess
import sys
from pathlib import Path
import integrate_20261010 as tool
from integrate_20261011_batches46_48 import item

def verify():
    p = tool.ROOT.parent / 'worktrees/dm8-compat-20261012/private-prepay-harness/review-old-refund242-independent/verify.py'
    assert hashlib.sha256(p.read_bytes()).hexdigest() == 'aa18c2b4b5ede830ac9033b88bf17a835bfb1eab8769b44ef702231c8d54933b'
    r = json.loads(subprocess.check_output([sys.executable, '-B', str(p)]))
    assert r['pass'] and r['independent_checks'] == 94 and r['author_checks'] == 144
    assert r['new_SQL'] == r['new_HTTP'] == 0 and r['full_refund_columns'] == 18

if __name__ == '__main__':
    audit = Path(__file__).with_name('integration-20261014-refund242-recovery.json')
    assert not audit.exists(), 'Never replay main'
    report = 'work/2026-09-28-smart-canteen-dm8-poc/matrix-integration/ROOT_REFUND242_RECOVERY_20261014.md'
    tool.main([
        item('ai_api', 'POST', '/api/refund/apply', '', evidence=report,
            scope='historical-owned-zero-money-refund-application-not-real-funds-refund',
            success_case='partial-evidence-historical-original-application-I1-complete18-column-readback',
            readback='tested-pass-historical-owned-Refund18-and-related-protection-hashes-no-full-SQL-ledger',
            parity='partial-evidence-complete-body-native-types-trace-only-difference-headers-not-preserved',
            cleanup='old-Refund3-retained-successful-apply-never-replayed-token-removed'),
        item('ai_api', 'POST', '/api/refund/delivery', '', evidence=report,
            scope='historical-WAIT-rejection-only-no-successful-shipping-or-logistics-write',
            mysql_state='partial-evidence-WAIT-negative-only', dm_state='partial-evidence-WAIT-negative-only',
            success_case='not-tested-positive-delivery-only-original-WAIT-rejection',
            readback='tested-pass-historical-observed-no-DML-protected-row-hashes-unchanged',
            parity='partial-evidence-complete-original-business500-WAIT-rejection-body-not-product-failure',
            cleanup='original-old-rows-retained-no-write-replay')
    ], audit, verify)
