#!/usr/bin/env python3
"""Root-only integration of AI Dishes empty-recipe bounded write 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-dishes-empty-recipe-exec-20261011.sSnDpT/resume-v2-final-seal')
EVIDENCE = 'work_ai_api/2026-09-30-dm8-interface-acceptance/parallel-ai-dishes-empty-recipe-20261011/ROOT_REVIEW_DISHES_EMPTY_RECIPE_20261011.md'


def verify():
    assert (tool.ROOT / EVIDENCE).is_file()
    manifest_bytes = (PRIVATE / 'MANIFEST.json').read_bytes()
    assert hashlib.sha256(manifest_bytes).hexdigest() == 'e2022c33d8f0074e09c06f2f1eb66813d6c27de00ed830752999f1b87e50ea99'
    result = json.loads(subprocess.run([sys.executable, '-B', str(PRIVATE / 'verify.py')],
        check=True, capture_output=True, text=True).stdout)
    assert result['assets'] == 127 and result['checks'] == 185
    assert result['B_EDIT'] == 'PASS_LIMITED_EMPTY_RECIPE' and result['C_ADD'] == 'PASS_LIMITED_EMPTY_RECIPE'
    assert result['C_EDIT_HTTP'] == 0 and result['old_B_ADD'] == 'PARTIAL_UNCHANGED'
    # Root-specific private receipts distinguish B's durable add from its later, complete edit.
    b = json.loads((PRIVATE / 'B-private' / 'after-add.json').read_text())
    assert len(b['dishes']) == 1 and len(b['dishes'][0]) == 59
    assert not (PRIVATE / 'B-private' / 'oracle-add.json').exists()


def targets():
    base = EVIDENCE.rsplit('/ROOT_REVIEW', 1)[0] + '/'
    out = [item('ai_api', 'POST', '/store/dishes.Dishes/add', base, evidence=EVIDENCE,
        from_source='current-AI-Store-client-controller-source', terminal='AI-Store-merchant-PC',
        role='existing-marked-merchant-operator', risk='owned-dish-main-row-empty-recipe-limited',
        scope='AI-primary-exact-own-tenant-dishes-main-row-no-relation-or-tag-write',
        request_type='original-JSON-form-empty-foods-and-unsuitable-arrays',
        success_case='partial-evidence-B-durable-insert-add-postread-tool-partial-C-full-empty-recipe-add',
        mysql_state='tested-pass', dm_state='tested-pass',
        readback='partial-evidence-B-full-own59row-independent-read-but-original-postCalculate-relation-SELECT-missing; C-original-two-postcommit-SELECTs-and-empty-fetch-complete',
        parity='partial-evidence-original-add-envelope-and-ID-string-types-per-lane-own-sequences-differ-empty-recipe-only',
        cleanup='retained-own-marked-active-dish-no-relation-tag-write-workers-token-cache-released',
        missing_required='not-tested', invalid_type='not-tested', duplicate_retry='not-tested',
        boundary_empty_min_max='partial-evidence-genuine-empty-recipe-caller-ignores-calculateFalse-not-nutrition-success', performance='not-tested'),
        item('ai_api', 'POST', '/store/dishes.Dishes/edit', base, evidence=EVIDENCE,
        from_source='current-AI-Store-client-controller-source', terminal='AI-Store-merchant-PC',
        role='existing-marked-merchant-operator', risk='owned-dish-weight-only-empty-recipe-limited',
        scope='AI-primary-exact-own-B-dish-PK-tenant-only-C-edit-not-tested',
        request_type='original-JSON-form-response-string-ID-and-empty-recipe-arrays',
        success_case='limited-B-only-own-row-weight-edit-on-empty-recipe-path',
        mysql_state='tested-pass', dm_state='not-tested-no-C-edit',
        readback='limited-B-full59row-and-two-real-postcommit-SELECTs-empty-fetch-stop-read; C-no-edit',
        parity='limited-single-engine-edit-only-no-two-engine-edit-parity-claim',
        cleanup='retained-own-B-dish-weight101.50-C-dish-untouched-no-related-row-worker-token-cache-released',
        missing_required='not-tested', invalid_type='not-tested', duplicate_retry='not-tested',
        boundary_empty_min_max='limited-B-only-weight100-to101.50-empty-recipe', performance='not-tested')]
    return out


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