#!/usr/bin/env python3
"""S1 bounded intake: DB positives, file/negative limits and one original failure."""
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
ROOT = Path('/Users/wiscourper/Documents/workspace/local/dm8-independent-20261013/session-store-nutrition')
REVIEW = Path('/private/tmp/cdx-ai-hk-course-b.Kmdirn/review-s1-301-309')
REPORT = tool.STORE + 'parallel-s1-nutrition-20261013/ROOT_REVIEW.md'


def verify():
    for name, sha in {
        'verify_s1_bundle.py': '9cb016cf5c215fd281ede548212186df1502bd5720c0c57bae2e40a3c6e73d27',
        'input-manifest-pins.json': '3b1783204be8aedbb41f8d1658ed287b721d6f92bffda4f875f598c9f98112e0',
    }.items():
        assert hashlib.sha256((REVIEW / name).read_bytes()).hexdigest() == sha
    r = json.loads(subprocess.check_output([sys.executable, '-B', str(REVIEW / 'verify_s1_bundle.py'), '--include-boundaries']))
    assert r['checks'] == 35959 and r['assets'] == 1223 and r['original_wire_count'] == 69
    assert len(r['unique_keys']) == 13 and len(r['empirical_boundaries']) == 4
    for script, checks in [('verify.py', 1942), ('batch309/verify.py', 500), ('batch312/verify.py', 1021)]:
        result = json.loads(subprocess.check_output([sys.executable, '-B', str(ROOT / script), '--manifest']))
        assert result['status'] == 'pass' and result['checks'] == checks
    for commit in ['d79395f07982a23b7b3c35c6890269c72a60c009', 'ef105f9b00b9b1f764acf1f218791b97869afcd5']:
        subprocess.check_call(['git', '-C', str(tool.source_root('store')), 'merge-base', '--is-ancestor', commit, 'develop_dameng'])


if __name__ == '__main__':
    audit = HERE / 'integration-20261013-s1-nutrition.json'
    assert not audit.exists(), 'Never replay integration main'
    positive = ['/p/birthdaydish/save', '/p/birthdaydish/batchsave', '/p/birthdaydish/import',
                '/p/birthdaydish/lists', '/p/birthdaydish/dishoptions', '/p/datemenu/editmenu',
                '/p/metabolismpal/querylistupdate', '/p/metabolismpal/findquerylist',
                '/m/weeknew/weeknewforscreen']
    rows = [item('store', 'POST', path, '', evidence=REPORT,
        success_case='partial-evidence-original-owned-business-chain-see-exact-batch-and-open-boundaries',
        request_type='actual-form-JSON-or-multipart-XLSX-see-report',
        readback='tested-pass-independent-native-rows-and-complete-response-types-values-not-general-normalization',
        parity='partial-evidence-source-derived-JSON-and-physical-FLOAT31-original-reds-preserved',
        cleanup='marked-fixtures-retained-workers-stopped-no-natural-row-or-external-mutation') for path in positive]
    rows[-1].update(register_new_runtime_key=True, runtime_observed_method='POST',
        runtime_inventory_scope='controller-direct-http-observed', runtime_direct_http_proof=REPORT,
        runtime_controller_source='application/m/controller/WeekNew.php:34 original-default-dispatch-no-current-client-claim')
    for method, path, boundary in [('GET', '/p/birthdaydish/template', 'real-XLSX-file-only'),
                                  ('POST', '/p/datemenu/getmenu', 'missing-person-negative-only'),
                                  ('POST', '/m/weeknew/weeknew', 'empty-personal-menu-and-source-specific-actor-values')]:
        rows.append(item('store', method, path, '', evidence=REPORT,
            mysql_state='partial-evidence-' + boundary, dm_state='partial-evidence-' + boundary,
            success_case='partial-evidence-limited-' + boundary,
            parity='partial-evidence-exact-limited-branch-see-report',
            readback='partial-evidence-no-business-source-mutation-' + boundary))
    rows.append(item('store', 'POST', '/p/datemenu/getlist', '', evidence=REPORT,
        mysql_state='tested-fail-original-method-missing', dm_state='tested-fail-original-method-missing',
        success_case='failed-original-method-missing-both-engines-not-implemented-for-counts',
        readback='partial-evidence-no-business-mutation', parity='failed-original-method-missing-both'))
    tool.main(rows, audit, verify)
