#!/usr/bin/env python3
"""Root one-time type0 message+feedback intake; no real device/MQ claim."""
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
REVIEW = tool.ROOT.parent / 'worktrees/dm8-compat-20261012/private-prepay-harness/review-s2-terminal441-459'
REV = '049d06328f74020544f8c061de14bf8050a299b7'
REPORT = tool.AI + 'parallel-terminal-face-20261013/ROOT_REVIEW.md'


def verify():
    for name, digest, count in [
        ('verify_author_readonly.py', '4ff7a43fd5a2ce149d13b20da9ab3834d5a6bc9837f60c64c537771033be59bb', 10418),
        ('verify_independent.py', 'f1b74d46d2c608f9b560571196405dc7dc7b14586da40d5077bdc714f3a75869', 829),
    ]:
        path = REVIEW / name
        assert hashlib.sha256(path.read_bytes()).hexdigest() == digest
        result = json.loads(subprocess.check_output([sys.executable, '-B', str(path)]))
        assert result.get('pass', result.get('passed')) and result['checks'] == count
    for path, digest in [
        ('app/terminal/service/staff/Face.php', '2c01498e76a6c3b3318470b8ca53393f2a39bb03dcd8c8c30e1f13ced1f2e30b'),
        ('tests/TerminalFaceIncrementalContract.check.php', 'e8d848262f16b420b12c6a2f77cbc4143b7fdd52ae5c68f906fcb9edae7d31f8'),
    ]:
        data = subprocess.check_output(['git', '-C', str(tool.source_root('ai_api')), 'show', REV + ':' + path])
        assert hashlib.sha256(data).hexdigest() == digest
    subprocess.check_call(['git', '-C', str(tool.source_root('ai_api')), 'merge-base', '--is-ancestor', REV, 'develop_dameng'])


if __name__ == '__main__':
    audit = HERE / 'integration-20261013-terminal-face.json'
    assert not audit.exists(), 'Never replay main'
    rows = [item('ai_api', method, '/terminal/store.face/' + action, '', evidence=REPORT,
        risk='own-new-Store-fake-device-message-status-only-no-real-device-MQ',
        success_case='partial-evidence-original-type0-incremental-or-single-feedback-and-empty-noop-branches',
        readback='tested-pass-complete-own-message-status-clock-and-Store-equipment-protection',
        parity='partial-evidence-full-JSON-values-types-order-private-store-context-not-real-tenant-selection',
        cleanup='owned-marker-fixtures-retained-private-workers-stopped-original-reds-preserved')
        for method, action in [('GET', 'facedata'), ('POST', 'feedback')]]
    tool.main(rows, audit, verify)
