#!/usr/bin/env python3
"""Enhance the two existing recharge routes; preserve cash and old points failures."""
import hashlib
import json
import subprocess
from pathlib import Path
import integrate_20261010 as tool
import integrate_20261012_batch142 as cash
from integrate_20261011_batches46_48 import item

HERE = Path(__file__).resolve().parent
PRIVATE = Path('/private/tmp/cdx-ai-hk-course-b.Kmdirn')
BASE = 'work_ai_api/2026-09-30-dm8-interface-acceptance/parallel-points-input-20261012/'


def verify():
    cash.verify()
    for name, digest in [
        ('POINTS_TEXT_INPUT_REPAIR_MANIFEST_20261012.json', '61568d943f2aeef996715b3522da296e2dd05a15062fd8a7d04506ad6954d944'),
        ('POINTS_TEXT_INPUT_REPAIR_BC_20261012.md', '8960bd1be2891ce7152312d091ac9a257e25d667f7cd5204470244e8f4f38127'),
        ('verify_points_repair_receipts.php', '224762a32488d7c1d6d58725fd4d5f543cb7d1b9ea9388090f8939152dbd5e62'),
        ('verify_points_text_red.php', 'ec2e1f2db394d642430504ff84fd2e6600d553622e639d6c9f800ae0fb0c1548'),
        ('run_points_owned_v2.php', 'fff254c9589fc36c3cf4716ab7eab8338954044b66b53743ac0aac7feb6c52dc'),
    ]:
        assert hashlib.sha256((PRIVATE / name).read_bytes()).hexdigest() == digest
    run = lambda name: json.loads(subprocess.check_output(['/opt/homebrew/opt/php@7.4/bin/php', str(PRIVATE / name)]))
    old = run('verify_points_text_red.php')
    assert old['status'] == 'verified_shared_baseline_failure' and old['business_http_total'] == 2
    assert old['business_dml_from_http'] == 0 and all(c['pass'] for c in old['cases'].values())
    new = run('verify_points_repair_receipts.php')
    assert new['status'] == 'verified' and new['twelve_real_business_http'] == 12
    assert new['new_unique_interface_keys'] == 0 and all(new['per_surface_full_response_parity_excluding_trace'].values())
    for c in new['cases'].values():
        assert c['pass'] and all(x['pass'] for x in c['outcomes'].values())
    for surface, digest in [('store', 'f674783847a009c78d4f83588c0aecf716d9e2d1c9bc938746c37091dee235d5'),
                            ('terminal', '5d0b76ba95537de952cf6c430ede66f67b5166797e6fc466abdc16ea423c06ea')]:
        assert hashlib.sha256((tool.source_root('ai_api') / 'app' / surface / 'model/User.php').read_bytes()).hexdigest() == digest


if __name__ == '__main__':
    audit = HERE / 'integration-20261012-batch146.json'
    assert not audit.exists()
    specs = [item('ai_api', 'POST', '/' + surface + '/User/recharge', BASE,
        risk='own-customer-internal-cash-and-points-no-external-payment',
        request_type='application/json-form-target-balance-or-points-query-see-both-reports',
        readback='tested-pass-own-cash-balance-ledger-and-points-ledger-readback',
        success_case='partial-evidence-balance-inc-0.01-and-points-inc-text-one-zero',
        invalid_type='partial-evidence-points-text-negative-one-rejected-other-invalid-unverified',
        boundary_empty_min_max='partial-evidence-points-text-zero-adds-zero-log-and-negative-one-no-write',
        parity='partial-evidence-full-business-response-cash-and-points-matched-cash-onecard-config-differs',
        cleanup='marked-cash-and-points-users-ledgers-retained-tokens-zero') for surface in ['store', 'terminal']]
    tool.main(specs, audit, verify)
