#!/usr/bin/env python3
"""Weight list: retain limited classification until populated measurement joins."""
import hashlib
import json
import re
import subprocess
from pathlib import Path
import integrate_20261010 as tool
from integrate_20261011_batches46_48 import item
from integrate_20261012_batch148 import shape

HERE = Path(__file__).resolve().parent
PRIVATE = tool.ROOT.parent / 'worktrees/dm8-compat-20261012/private-prepay-harness/batch158-weight-preflight'
BASE = 'work_store/apifox/2026-09-30-dm8-interface-acceptance/parallel-weight-fallback-read-20261012/'


def verify():
    report = PRIVATE / 'EXECUTION_REPORT.md'
    assert hashlib.sha256(report.read_bytes()).hexdigest() == 'bdc7ed84af5b0d48b9798689162a1aa64e160aadc8b6cb220240027518c56ba1'
    assert hashlib.sha256((PRIVATE / 'REPORT.md').read_bytes()).hexdigest() == '4542d0fb36fdbb8952a4f663830e1c6ad61bee463c094788e6b8cf1f61d4ddd4'
    for file, digest in re.findall(r'\| `([\w.-]+)` \| `([0-9a-f]{64})` \|', report.read_text()):
        assert hashlib.sha256((PRIVATE / file).read_bytes()).hexdigest() == digest
    for path, digest in [
        ('application/p/controller/Weight.php', 'd5f1b8914e862585dcc69cbab0a8a6d70a9d7a91141d63d932898e25d1702fb1'),
        ('application/p/logic/Weight.php', 'a35eb95fca42c71aa5f8f9506357432e92fc18baf3d956c5e2adb3b66f816803'),
        ('application/model/Weight.php', 'a092f1b802597cea7e429f9a3d5860d363538dc8c548f4261cbd7efff1b1ed05'),
    ]:
        assert hashlib.sha256((tool.source_root('store') / path).read_bytes()).hexdigest() == digest
    result = json.loads(subprocess.check_output(['docker', 'exec', 'zhctapp_v2_0', '/usr/local/php7.3/bin/php',
        '/workspace/zhct/worktrees/dm8-compat-20261012/private-prepay-harness/batch158-weight-preflight/verify_offline.php']))
    assert result['status'] == 'verified-positive-empty-join-branch' and result['http'] == 2
    bodies = []
    for lane in ['B', 'C']:
        body = json.loads((PRIVATE / (lane + '-weight158-lists.raw')).read_text())
        assert isinstance(body['data'], dict) and isinstance(body['data']['rows'], list)
        assert len(body['data']['rows']) == 1 and type(body['data']['total']) is int and body['data']['total'] == 1
        row = body['data']['rows'][0]
        assert isinstance(row.pop('uuid'), str) and isinstance(row.pop('name'), str)
        bodies.append(body)
    assert bodies[0] == bodies[1] and shape(bodies[0]) == shape(bodies[1])


if __name__ == '__main__':
    audit = HERE / 'integration-20261012-batch158.json'
    assert not audit.exists()
    specs = [item('store', 'POST', '/p/weight/lists', BASE,
        mysql_state='partial-evidence-nonempty-staff-empty-measurement-join-fallback-only',
        dm_state='partial-evidence-nonempty-staff-empty-measurement-join-fallback-only',
        success_case='partial-evidence-staff-fallback-HTTP200-code0-nonempty1-populated-weight-join-not-tested',
        readback='partial-evidence-full-staff-department-link-unchanged-own-weight-status-zero',
        parity='partial-evidence-complete-nonidentity-JSON-equal-in-empty-measurement-join-only',
        performance='partial-evidence-B0.7771s-C0.1345s-small-indexed-staff-sample-not-large-table-proof',
        cleanup='own-department-and-staff-link-retained-visible-no-Staff-or-natural-row-update')]
    tool.main(specs, audit, verify)
