#!/usr/bin/env python3
"""Root-only integration of AI Foods/edit limited missing-row read evidence."""
import hashlib
import json
from pathlib import Path
import subprocess
import sys

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-ai-food-edit-missing-exec-20261011.m47psf/final-seal-v2')
EVIDENCE = 'work_ai_api/2026-09-30-dm8-interface-acceptance/parallel-ai-foods-edit-negative-20261011/ROOT_REVIEW_FOODS_EDIT_NEGATIVE_20261011.md'


def verify():
    assert (tool.ROOT / EVIDENCE).is_file()
    manifest = (PRIVATE / 'MANIFEST.json').read_bytes()
    assert hashlib.sha256(manifest).hexdigest() == 'be829fc0f7ef9ef458a8693a5e8a12cc895d0b9edb280689845fb3f49e63b168'
    result = json.loads(subprocess.run([sys.executable, '-B', str(PRIVATE / 'verify.py')],
        check=True, capture_output=True, text=True).stdout)
    assert result['assets'] == 95 and result['checks'] == 156 and result['result'] == 'LIMITED_NEGATIVE_BOTH'
    assert result['HTTP'] == 2 and result['DML'] == 0 and result['B_SQL_native'] == [20, 7] and result['C_SQL_native'] == [30, 17]


def targets():
    base = EVIDENCE.rsplit('/ROOT_REVIEW', 1)[0] + '/'
    return [item('ai_api', 'POST', '/store/dishes.Foods/edit', base, evidence=EVIDENCE,
        from_source='controller-action-source-current-frontend-edit-call-not-found', terminal='AI-Store-merchant-PC-controller-direct-negative',
        role='existing-marked-merchant-operator-original-auth', risk='soft-deleted-own-food-row-negative-select-only',
        scope='AI-primary-own-soft-deleted-food-PK-and-tenant-read-only-no-controller-DML',
        request_type='original-JSON-response-string-ID-plus-full-valid-form',
        success_case='limited-negative-original-detail-filter-is_delete0-excludes-only-owned-softdeleted-row',
        mysql_state='tested-pass-negative', dm_state='tested-pass-negative',
        readback='limited-successful-native-SELECT-and-original-fetchAll-empty-plus-stop-full55row-unchanged',
        parity='limited-identical-original-not-found-envelope-per-engine-own-PK-values-strings-no-frontend-edit-call',
        cleanup='own-softdeleted-row-and-operator-unchanged-own-token-cache-worker-released',
        missing_required='not-tested', invalid_type='not-tested', duplicate_retry='not-tested',
        boundary_empty_min_max='limited-softdeleted-record-is-not-found-no-positive-edit', performance='not-tested')]


if __name__ == '__main__':
    audit = HERE / 'integration-20261011-batch102.json'
    assert not audit.exists(), 'Preserve previous integration evidence'
    tool.main(targets(), audit, verify)
