#!/usr/bin/env python3
"""Root-only integration of Store bill/update_list empty-recipe read evidence."""
import hashlib
import json
from pathlib import Path
import subprocess

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-store-bill-update-list-actual-20261011.4W6fF3')
EVIDENCE = 'work_store/apifox/2026-09-30-dm8-interface-acceptance/parallel-store-bill-update-list-20261011/ROOT_REVIEW_BILL_UPDATE_LIST_20261011.md'


def verify():
    assert (tool.ROOT / EVIDENCE).is_file()
    report = (PRIVATE / 'REPORT.md').read_bytes()
    assert hashlib.sha256(report).hexdigest() == '63d7966d63c80c9b131c9d8a9aa28d8c978a0a501eb61bfaa781f82420051a0b'
    manifest = (PRIVATE / 'manifest.json').read_bytes()
    assert hashlib.sha256(manifest).hexdigest() == '72dc66e404b53761fd241fe72e4cc44c538beab773b200ff75195e5e69fcd00e'
    result = json.loads(subprocess.run(['node', str(PRIVATE / 'verify.js'), '--readonly'],
        check=True, capture_output=True, text=True).stdout)
    assert result['checks'] == 1505 and result['assets'] == 86
    assert result['manifest_SHA'] == '72dc66e404b53761fd241fe72e4cc44c538beab773b200ff75195e5e69fcd00e'
    assert all(x['HTTP'] == 1 and x['DML_DDL_txn_AI_external'] == 0
        for x in result['sides'].values())
    assert result['sides']['B']['new_reads'] == 55 and result['sides']['C']['new_reads'] == 72
    assert result['sides']['B']['fetch'] == [0, 5464, 0] and result['sides']['C']['fetch'] == [0, 2008, 0]
    assert all(x['RAM_full_typed_DTO_match'] and x['protected_same'] and x['own_resources_clean']
        for x in result['sides'].values())


def targets():
    base = EVIDENCE.rsplit('/ROOT_REVIEW', 1)[0] + '/'
    return [item('store', 'POST', '/p/bill/update_list', base, evidence=EVIDENCE,
        terminal='current-PC-recipe-management-editor', role='existing-marked-normal-admin-auth-and-restaurant-permission',
        risk='readonly-four-meal-menu-editor-large-catalog-response-empty-future-recipe-date',
        scope='Store-primary-marked-restaurant78-C13-active-dish-catalog-per-side-selected-storeid10001-no-source-tenant-filter',
        request_type='actual-urlencoded-menu_date-and-restaurant_id',
        mysql_state='tested-pass', dm_state='tested-pass',
        readback='partial-evidence-three-original-SELECT-fetch-counts-and-full-typed-RAM-DTo-equal-own-source-per-engine',
        parity='partial-evidence-own-source-response-matches; B-C natural catalog snapshots not compared',
        cleanup='read-only-empty-future-recipe-date-own-marked-restaurant-actor-cache-token-worker-clean-source-projection-unchanged',
        success_case='partial-evidence-empty-future-recipe-date-with-nonempty-catalog-and-original-four-meal-shape',
        missing_required='not-tested', invalid_type='not-tested', duplicate_retry='not-applicable-read-only',
        boundary_empty_min_max='partial-evidence-only-empty-menu-date-nonempty-selection-and-other-dates-not-tested', performance='not-tested')]


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