#!/usr/bin/env python3
"""RO8 seven real keys, four limited; add immutable SystemConfig repair evidence."""
import hashlib
import json
import subprocess
from pathlib import Path
import integrate_20261010 as tool
from integrate_20261011_batches46_48 import item

HERE = Path(__file__).resolve().parent
PRIVATE = Path('/Users/wiscourper/Documents/workspace/local/dm8-store-purchase-read3-20261012.L5dTYR/stage3')
FIX = PRIVATE / 'systemconfig-date-candidate-20261012'
BASE = 'work_store/apifox/2026-09-30-dm8-interface-acceptance/parallel-store-ro8-20261012/'


def verify():
    for path, digest in [
        (PRIVATE / 'manifest-ro8.json', 'd4de0bc49cda46b7bb6a90e9de95d6a842990dc877cb0fd495a230ac7ba0e9bd'),
        (PRIVATE / 'audit-ro8.js', 'bf7231a95b3d2d3478a848b58ff7f458d7adc6d68673eb3b5ea9f417673d9094'),
        (FIX / 'manifest.json', 'cae6e295159b492dbcea6fbd9f6991811c0e841e410b1753fe15f7c10a8b84ca'),
        (FIX / 'verify.js', '051e78883e66bf029f5242ff790b3afeb2f3497e9ef9d3d3d39257f147cc17cc'),
        (tool.source_root('store') / 'application/p/logic/SystemConfig.php', 'fa68a4674dcc9ba11ae7e4ff3438f5e043059ffd31693c2797c756c4b055f8ed'),
    ]:
        assert hashlib.sha256(path.read_bytes()).hexdigest() == digest
    original = json.loads(subprocess.check_output(['node', str(PRIVATE / 'audit-ro8.js'), '--readonly']))
    assert original['readonly'] and original['checks'] == 3142 and original['assets'] == 134
    assert original['HTTP'] == {'B': 8, 'C': 8}
    assert original['DML'] == original['DDL'] == original['transactions'] == 0
    fixed = json.loads(subprocess.check_output(['node', str(FIX / 'verify.js'), '--readonly']))
    assert fixed['readonly'] and fixed['checks'] == 332 and fixed['assets'] == 28
    for lane, http, native, changed in [('B', 2, 4, 0), ('C', 1, 6, 2)]:
        side = fixed['sides'][lane]
        assert side['actual_new_HTTP'] == http and side['success_HTTP'] == 1
        assert side['actual_native'] == native and side['date_fields_changed'] == changed
        assert side['actor_config_unchanged']


if __name__ == '__main__':
    audit = HERE / 'integration-20261012-batch131.json'
    assert not audit.exists()
    targets = []
    for path in ['/p/department/all', '/p/systemConfig/lists', '/p/PurchaseFlow/getUser',
                 '/p/signDaka/lists', '/p/WorkFlow/info', '/p/WorkFlow/approveList', '/p/staff/workflowLists']:
        spec = item('store', 'POST', path, BASE,
            from_repo='unreviewed', from_source='apifox-2026-05-08',
            scope='bounded-current-source-readonly-original-DB-rows-no-AI-external',
            parity='partial-evidence-each-engine-source-values-types-order-in-RAM-not-identical-natural-snapshots',
            cleanup='no-business-DML-private-workers-token-cache-intentionally-retained-for-reuse')
        if path in ['/p/signDaka/lists', '/p/WorkFlow/info', '/p/WorkFlow/approveList', '/p/staff/workflowLists']:
            spec.update(mysql_state='tested-pass-bounded-nonempty-source', dm_state='partial-evidence-empty-source-only',
                        readback='partial-evidence-B-nonempty-C-empty-no-positive-DM-rows')
        else:
            spec['readback'] = 'tested-pass-nonempty-original-source-readback-native-SQL-fetch-and-HTTP-RAM-proof'
        if path == '/p/systemConfig/lists':
            spec['parity'] = 'partial-evidence-DM-two-zero-fraction-date-fields-fixed-B-whole-body-unchanged-source-row-unchanged'
        targets.append(spec)
    tool.main(targets, audit, verify)
