#!/usr/bin/env python3
"""Four real scale readers after the narrow output fix, preserving original red seals."""
import hashlib
import json
from pathlib import Path
import subprocess
import sys
import integrate_20261010 as tool
from integrate_20261011_batches46_48 import item

BASE = tool.AI + 'parallel-ai-scale-datetime-read4-20261011/'

def verify():
    root = tool.ROOT / BASE
    assert hashlib.sha256((root / 'manifest.json').read_bytes()).hexdigest() == '27a2b569f0ab21b3fc910a274f7c52b00cf6e4dbbc53ce5557e390c252ab0683'
    r = json.loads(subprocess.run([sys.executable, '-B', str(root / 'audit.py')], check=True, capture_output=True, text=True).stdout)
    assert r['checks'] == 1298 and r['cross_fixed_QN_DTO_except_real_session_ID'] == 'PASS'
    for lane, sql in [('B', 173), ('C', 287)]:
        f = r['lanes'][lane]
        assert f['HTTP'] == f['full_DTO_pass'] == 4 and f['prepare'] == f['DMLDDL'] == 0
        assert f['SQL'] == sql and f['all_date_strings19'] and f['all10physical_rows_unchanged']
        assert f['ownAI_funds_protection'] and not f['global_StoreStaff_claim'] and f['resources_cleaned']
    s = json.loads(subprocess.run([sys.executable, '-B', str(root / 'verify_seal.py')], check=True, capture_output=True, text=True).stdout)
    assert s['all_seals_immutable'] and s['verified_assets'] == 1411
    pins = json.loads((root / 'source-frozen-sha.json').read_text())['files']
    approved = tool.load('work/2026-09-28-smart-canteen-dm8-poc/matrix-integration/AI_RUNTIME_SOURCE_20261011_1230.json')['files']
    assert pins == approved and len(pins) == 6169
    for name, digest in pins.items():
        assert hashlib.sha256((tool.ROOT.parent / 'zhctproject/ai_api' / name).read_bytes()).hexdigest() == digest

def targets():
    out = []
    for vendor, action, method in [('WellandScale', 'byDate', 'GET'),
                                   ('YunkangbaoScale', 'byDate', 'GET'),
                                   ('WellandScale', 'latest', 'POST'),
                                   ('YunkangbaoScale', 'latest', 'GET')]:
        extra = dict(request_type='application/json-empty-object' if method == 'POST' else 'GET-date-query-or-empty',
            scope='customer-Store-secondary-nonempty-session-raw-health-whole-source-readonly',
            risk='targeted-DATETIME0-measured_at-contract-no-physical-change',
            success_case='partial-evidence-original-nonempty-ten-field-DTO-and-source-native-types',
            parity='partial-evidence-B-original-whole-contract-unchanged-C-zero-fraction-output-aligned-fixed-QN-whole-data-equal-except-generated-ID',
            cleanup='no-new-business-rows-original10-measurement-and-ownAI-funds-unchanged-workers-token-cache-cleared')
        if method == 'POST':
            extra.update(register_new_runtime_key=True, runtime_observed_method='POST',
                runtime_client_source='zhctproject/ai_app/api/weightManage.js:45 request.post(api.weightLatest)',
                runtime_controller_source='zhctproject/ai_api/app/api/controller/WellandScale.php:68 original latest',
                terminal='mobile-ai_app-current-POST-not-replacement-for-old-GET')
        out.append(item('ai_api', method, '/api/' + vendor + '/' + action, BASE, **extra))
    return out

if __name__ == '__main__':
    audit = Path(__file__).with_name('integration-20261011-batch90.json')
    assert not audit.exists(), 'Preserve original and repaired transition audits'
    tool.main(targets(), audit, verify)
