#!/usr/bin/env python3
"""Single intake of audited Auth/plate/equipment and Store add/soft-delete evidence."""
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-s2-auth-and-sealed-backlog-20261013/ROOT_REVIEW.md'
AUTH = 'e10f70a3a947879d00f7538c14da8f62fada6a66'
CHECKS = [
    ('review-s2-349-359/verify_review.py', '1d773aded048a055cfd289ca954d203334ae251da4375b07ffd4d95f8664a9e2', 'checks', 1995),
    ('review-s2-336-348/verify_s2_bundle.py', '975ce091ee1ded1171b232da2da3afcded0f781be02bf515e1e2f657a94376e8', 'supplemental_checks', 1695),
    ('review-ai-251/verify_review.py', 'cae56d5e12c6e9fe030850bc1b6b2131e0425496cb2c0cb431290346ab0cc8bd', 'checks', 397),
    ('review-store-268-280/verify_review.py', '6089a8ab81c50a7b1b9939c1293693a26df16bcedc793784c2962365c9935105', 'checks', 321),
]


def verify():
    for relative, digest, count_field, count in CHECKS:
        script = PRIVATE / relative
        assert hashlib.sha256(script.read_bytes()).hexdigest() == digest
        result = json.loads(subprocess.check_output([sys.executable, '-B', str(script)]))
        assert result.get('verified', result.get('offline_verified')) and result[count_field] == count
    subprocess.check_call(['git', '-C', str(tool.source_root('ai_api')), 'merge-base', '--is-ancestor', AUTH, 'develop_dameng'])
    source = subprocess.check_output(['git', '-C', str(tool.source_root('ai_api')), 'show', AUTH + ':app/api/controller/yingjiduo/Auth.php'])
    assert hashlib.sha256(source).hexdigest() == '5fe5421adb18f6b35aa4179549440dc16b1509aab0201f5200bfb6a64880241e'


def evidence(repo, method, path, scope):
    return item(repo, method, path, '', evidence=REPORT,
        request_type='actual-source-form-JSON-multipart-or-GET-see-report',
        success_case='partial-evidence-' + scope,
        readback='tested-pass-complete-owned-native-row-state-and-real-driver-journal-see-group',
        parity='partial-evidence-complete-raw-JSON-type-value-contract-and-specific-branch-only',
        cleanup='owned-marked-records-retained-or-originally-deleted-see-group-no-real-device-storage-payment')


if __name__ == '__main__':
    audit = HERE / 'integration-20261013-auth-and-backlog.json'
    assert not audit.exists(), 'Never replay main'
    rows = [evidence('ai_api', method, '/api/yingjiduo.auth/' + action, 'original-owned-QR-login-latest-logout-and-expired-pending')
            for method, action in [('GET', 'userLoginQrURL'), ('GET', 'userLoginResult'), ('POST', 'qrConfirm'), ('POST', 'userLogout')]]
    rows.append(evidence('ai_api', 'POST', '/api/plate/bindByScan', 'real-AI-to-loopback-Store-own-plate-and-zero-order-transaction'))
    rows += [evidence('ai_api', method, '/admin/store.Equipment/' + action, 'original-disabled-unpaired-device-JSON-numeric-id-only')
             for method, action in [('POST', 'add'), ('POST', 'edit'), ('GET', 'getCode'), ('POST', 'delete'), ('GET', 'detail')]]
    rows += [evidence('store', 'POST', path, scope) for path, scope in [
        ('/p/article/add', 'original-multipart-and-real-DB-add-lowest-upload-substitution'),
        ('/p/bill/dishesRecognizeImgDelete', 'original-single-owned-image-metadata-soft-delete-no-files')]]
    for action in ('running', 'strength', 'bicycle'):
        row = evidence('ai_api', 'POST', '/api/yingjiduo.record/' + action, 'additional-sport-input-boundaries')
        row['evidence_only'] = True
        rows.append(row)
    tool.main(rows, audit, verify)
