#!/usr/bin/env python3
"""Root-only integration of one exact AI Store feedback status update."""
import hashlib
import json
from pathlib import Path
import subprocess
import sys

import integrate_20261010 as tool
from integrate_20261011_batches46_48 import item

PRIVATE = Path('/Users/wiscourper/Documents/workspace/local/dm8-ai-feedback-status1-exec-20261011.fucdvB')
AI_REPO = tool.ROOT.parent / 'zhctproject/ai_api'
EVIDENCE = 'work_ai_api/2026-09-30-dm8-interface-acceptance/parallel-ai-feedback-status1-20261011/ROOT_REVIEW_FEEDBACK_STATUS_SET_20261011.md'


def verify():
    assert (tool.ROOT / EVIDENCE).is_file()
    manifest = json.loads((PRIVATE / 'manifest.json').read_text())
    assert hashlib.sha256((PRIVATE / 'manifest.json').read_bytes()).hexdigest() == 'a42ea63a94b1b1b28357ca44a156d20b17664109f507c7de37c00a4352b31065'
    assert manifest['scope'].startswith('A1 exact two once-only original POSTs/one UPDATE each')
    result = subprocess.run([sys.executable, '-B', str(PRIVATE / 'verify.py')],
        check=True, capture_output=True, text=True)
    verification = json.loads(result.stdout)
    assert verification['pass'] and verification['assets'] == 167 and verification['sha_checks'] == 167
    audit = verification['audit']
    assert audit['pass'] and audit['checks'] == 12778
    for lane, owner, sql_total, reads, native in [('B', 974, 24, 23, 10), ('C', 1192, 35, 34, 21)]:
        s = audit['summary'][lane]
        assert s['HTTP'] == s['UPDATE'] == s['affected'] == 1
        assert s['commit'] and s['status_delta'] == '1→2' and s['full_DTO13'] and s['cleanup']
        assert s['SQL_total'] == sql_total and s['SQL_read_only'] == reads and s['native_total'] == native
        assert s['old_owner'] + reads + (sql_total - reads) == owner
    assert verification['DB'] == verification['HTTP'] == 0
    assert subprocess.run(['git', '-C', str(AI_REPO), 'rev-parse', 'HEAD'],
        check=True, capture_output=True, text=True).stdout.strip().startswith('72cf948d')


def targets():
    return [item('ai_api', 'POST', '/store/user.UserFeedback/set', EVIDENCE.rsplit('/ROOT_REVIEW', 1)[0] + '/',
        evidence=EVIDENCE,
        from_source='controller-action-source',
        terminal='AI-Store-merchant-PC', role='existing-authorized-marked-merchant-operator',
        risk='single-marked-feedback-status-update',
        scope='AI-primary-exact-existing-marked-feedback-PK-tenant-only',
        request_type='application/json-integer-id-and-status',
        success_case='partial-evidence-one-marked-feedback-status1-to2-update-per-engine',
        mysql_state='tested-pass', dm_state='tested-pass',
        readback='tested-pass-full-original-feedback-user-operator-rows-after-stop',
        parity='partial-evidence-original-13-field-DTO-and-engine-source-native-types-verified-per-lane-no-global-snapshot-equality',
        cleanup='retained-marked-feedback-status2-actor-rows-unchanged-worker-token-cache-released',
        missing_required='not-tested', invalid_type='not-tested', duplicate_retry='not-tested',
        boundary_empty_min_max='not-tested', performance='not-tested')]


if __name__ == '__main__':
    audit = Path(__file__).with_name('integration-20261011-batch96.json')
    assert not audit.exists(), 'Preserve any previous integration evidence'
    tool.main(targets(), audit, verify)
