#!/usr/bin/env python3
"""Offline assertions over actual synthetic DM CLI evidence, no database calls."""
import hashlib
import json
from pathlib import Path
ROOT=Path(__file__).resolve().parent
EXPECTED={'IteratorShadowStatement.php':'cd85349c1dba01bf7a9a0e1b84875f0177bddb128909a43d0f9f2afc0483f188',
          'IteratorProbe.php':'1ed10c2cb6c8735e8e98bc7c5089eb314597c3b7dfc889574368782c941455c2',
          'RecoveryProbe.php':'66f0878367ba93a04cf9405cba8ef962d4f776a9da802d1de081dbedd3bb8f8e',
          'FrameworkProbe.php':'fba880fc6d0fdf02b595e065ce5d9a4ecf36f1dc367e08cff0202a916be56447'}
def verify(data):
    for stage,file,count in [('old','old-red.json',3),('green','iterator-green.json',662),('recovery','recovery-green.json',1381)]:
        x=data[file]
        assert x['stage']==stage and x['expected_red_verified' if stage=='old' else 'green_verified'] is True
        assert [r['php_key'] for r in x['runs']]==['73','74']
        for run in x['runs']:
            r=run['report']
            assert run['stderr_bytes']==0 and run['exit_code']==(1 if stage=='old' else 0)
            assert r['status']==('fail' if stage=='old' else 'pass') and r['assertions']==count
            assert r['stage']==('planned_decimal30_2_0_2' if stage=='old' else 'complete')
            assert r['dml']==r['ddl']==0
            if stage=='old':
                assert r['business_reads']==0 and r['class']=='RuntimeException'
                continue
            assert r.get('business_reads',0)==0
            assert r['identity']['int_size']==8 and r['identity']['extra_decimal_defined'] is False and r['identity']['core_soft']==0
            assert r['identity']['php'].startswith('7.3.' if run['php_key']=='73' else '7.4.')
            assert r['target']['schema_matches'] is True and r['target']['owned_established_loopback_5238_sockets']>=1
            expected=dict(r['identity']['components']);expected.pop('/www/server/php/'+run['php_key']+'/bin/php')
            assert expected==r['identity']['mapped']
            if stage=='green':
                assert r['class']=='IteratorShadowStatement' and r['global_precision_gate_closed'] is False
            else:
                assert r['iterator']['status']=='supported_observed'
                assert r['iterator']['rows']==r['iterator']['expected']==[{'k':1,'v':'9007199254740993.01'},{'k':2,'v':None}]
                assert r['real_orm_getter_tested'] is False
    source_gate=data['framework-source-gate-red.json'];old=data['framework-old-red.json'];green=data['framework-green.json']
    for x in [source_gate,old,green]:
        assert len(x['runs'])==1 and x['runs'][0]['php_key']=='74' and x['runs'][0]['stderr_bytes']==0
        assert x['runs'][0]['report']['dml']==x['runs'][0]['report']['ddl']==x['runs'][0]['report']['business_reads']==0
    # Preserve the original harness expectation mismatch rather than rewriting it.
    assert source_gate['expected_red_verified'] is False and source_gate['runs'][0]['report']['stage']=='identity'
    assert source_gate['runs'][0]['report']['assertions']==2 and source_gate['runs'][0]['exit_code']==1
    assert old['expected_red_verified'] is False and old['runs'][0]['report']['stage']=='framework_pdo_foreach'
    assert old['runs'][0]['report']['assertions']==124 and old['runs'][0]['exit_code']==1
    assert green['green_verified'] is True and green['runs'][0]['exit_code']==0
    r=green['runs'][0]['report']
    assert r['status']=='pass' and r['stage']=='complete' and r['assertions']==133
    assert r['numeric_scenarios']==28 and r['actual_framework_prepares']==30
    assert r['statement_class']=='IteratorShadowStatement' and r['actual_query_method_used'] is True
    assert r['actual_file_cache_cold_hot_verified'] is True and r['cache_files_remaining']==0
    assert r['synthetic_model_getter_only'] is True and r['real_business_ORM_getter_tested'] is False and r['builder_auto_projection_tested'] is False
    assert r['global_p0_closed'] is False and r['identity']==data['iterator-green.json']['runs'][1]['report']['identity']
    return {'status':'pass','PHP73_assertions':2043,'PHP74_assertions':2176,'global_precision_gate_closed':False,'business_DML':0}
if __name__=='__main__':
    for file,sha in EXPECTED.items():assert hashlib.sha256((ROOT/file).read_bytes()).hexdigest()==sha
    print(json.dumps(verify({f:json.loads((ROOT/f).read_text()) for f in ['old-red.json','iterator-green.json','recovery-green.json','framework-source-gate-red.json','framework-old-red.json','framework-green.json']})))
