#!/usr/bin/env python3
"""Root-reviewed existing export slices plus one owned JSON write key."""
import hashlib
import json
import subprocess
import sys
from pathlib import Path
import integrate_20261010 as tool
from integrate_20261011_batches46_48 import item

HERE = Path(__file__).resolve().parent
PRIVATE = Path('/private/tmp/cdx-ai-hk-course-b.Kmdirn')
REPORT = tool.AI + 'parallel-address-json-20261013/ROOT_REVIEW.md'
CHECKS = [
    ('review-s3-383-389', 'ef3c92e98162b611848270c8c26eea6c3afa93e906c1c922fa214127147c2fd5', 1925),
    ('review-s3-523-524', '02e42df19acadad32c3e39dc33d961d845be350e11ab27a571d0ce6fe3d22da3', 2615),
    ('review-s2-561-569', 'efde10dad27346853d660f9f73d009ac1fff74d93ea2a552e6779a6309fe5199', 568),
]

def verify():
    for name, digest, count in CHECKS:
        path = PRIVATE / name / 'verify_review.py'
        assert hashlib.sha256(path.read_bytes()).hexdigest() == digest
        result = json.loads(subprocess.check_output([sys.executable, '-B', str(path)]))
        assert result.get('offline', result.get('verified_bounded_evidence'))
        assert result.get('checks', result.get('supplemental_checks')) == count
        if name == 'review-s3-523-524':
            assert result['real_http'] == 12 and result['xlsx_data_cells'] == 448
            assert result['original_521_still_red']
        if name == 'review-s2-561-569':
            assert result['raw_http'] == 16 and result['real_DML'] == 8
            assert result['GET_natural_settings_row_reads'] == 0 and not result['business_patch']
    for revision in ['101367397e01d716b7069403c7f46479b60b23e4', 'b4dc2778b8a783652ab4119eb33d87cd48e299d6']:
        subprocess.check_call(['git', '-C', str(tool.source_root('ai_api')), 'merge-base', '--is-ancestor', revision, 'develop_dameng'])

if __name__ == '__main__':
    audit = HERE / 'integration-20261013-address-json.json'
    assert not audit.exists(), 'Never replay main'
    rows = [item('store', 'POST', '/p/' + controller + '/exportlogs', '', evidence=REPORT, evidence_only=True)
            for controller in ['cashoutorder', 'cashrefund', 'supplementmealorder']]
    rows += [item('store', method, path, '', evidence=REPORT, evidence_only=True) for method, path in [
        ('POST', '/p/cashoutorder/export'), ('GET', '/p/cashrefund/export'), ('POST', '/p/supplementmealorder/export')]]
    rows += [item('ai_api', 'POST', '/' + app + '/order.export/exportOrder', '', evidence=REPORT, evidence_only=True)
             for app in ['store', 'terminal']]
    rows.append(item('ai_api', 'GET', '/terminal/setting/detail', '', evidence=REPORT, evidence_only=True))
    rows.append(item('ai_api', 'POST', '/terminal/setting/update', '', evidence=REPORT,
        success_case='partial-evidence-owned-printer-disabled-short-JSON-insert-update-same-value-empty-string',
        readback='tested-pass-complete-six-native-columns-UTF8-four-byte-JSON-exact-bytes',
        parity='partial-evidence-full-typed-HTTP-short-JSON-not-long-CLOB-concurrency-or-other-keys',
        cleanup='own-Store-and-setting-retained-private-auth-cache-cleaned-no-real-print-or-natural-settings-write'))
    tool.main(rows, audit, verify)
