"""Conformance for authored case evidence and its stage-by-stage disclosure."""
from pathlib import Path
import json
from html import escape
from _paths import SITE,workspace_path
HERE=Path(__file__).resolve().parent

def main():
    content=json.loads((HERE/'case-content.json').read_text(encoding='utf-8'))
    traces=json.loads((HERE/'case-runtime-traces.json').read_text(encoding='utf-8'))
    failures=[];checks=0
    def check(name,ok):
        nonlocal checks
        checks+=1
        if not ok:failures.append(name)
    check('all 68 cases have authored content',set(content['cases'])==set(traces) and len(traces)==68)
    for cid,trace in traces.items():
        p=trace['journey']['ui']['content']
        for key in ['failure','solution','recovery','memory','skill','tool','basis','similar']:
            check(cid+' '+key,len(p.get(key,''))>35)
        for key in ['findings','steps','checks']:
            check(cid+' '+key,len(p[key])==3 and len(set(p[key]))==3 and all(len(s)>35 for s in p[key]))
        for source in p['sources']:check(cid+' source '+source['path'],workspace_path(source['path']).is_file())
        for uid,t in trace['stage_traces'].items():
            n=t['narrative']
            check(cid+' authored phase '+uid,all(n[k] for k in ['heading','body','agent_message','root_message','rows','sources']))
            check(cid+' bounded row summary '+uid,len(n['rows'])==3)
        paragraph=(workspace_path('aisa-next/10 Architecture')/next(c['source'] for c in json.loads((HERE/'case-journeys.json').read_text(encoding='utf-8'))['cases'] if c['id']==cid)).read_text(encoding='utf-8')
        check(cid+' catalogue carries same content','<!-- CASE-CONTENT:'+cid+' -->' in paragraph and escape(p['failure']) in paragraph)
    d=traces['DC-02'];st=d['stage_traces'];path=d['journey']['paths']['first']
    core=[st[s['uid']]['narrative'] for s in path if s['stage'] in ['S3','S2','S4','S5','S6']]
    check('six successive Data moments have distinct messages',len(core)==6 and len({n['agent_message'] for n in core})==6)
    first=core[0];invest=core[1];choice=core[2]
    check('first take does not assert the final registry diagnosis','only /1–/4' not in first['agent_message'] and 'contains /1–/4' not in first['root_message'])
    check('investigation establishes five-versus-four evidence','five' in invest['agent_message'] and 'row_id=5' in invest['agent_message'])
    check('return to take chooses the narrow remedy','single missing /5' in choice['body'])
    check('historic actor is retained',d['journey']['ui']['external_owner']=='Bulstrad IT')
    check('receipt and verification are separate',any(t['narrative']['phase']=='external-report' and not t['narrative']['proof'] for t in st.values()))
    check('template example is explicitly composite','Composite' in traces['CF-05']['journey']['ui']['content']['basis'])
    check('all ten historical studies retain their own content',set(content['studies'])=={str(i) for i in range(1,11)})
    historical=workspace_path('aisa-next/10 Architecture/40 Support/Case Studies.md').read_text(encoding='utf-8')
    check('all ten study blocks generated',all('<!-- STUDY-CONTENT:'+str(i)+' -->' in historical for i in range(1,11)))
    check('mirror completion is not turned into an execution','not executed' in content['studies']['10']['findings'][2])
    index=(SITE/'index.html').read_text(encoding='utf-8')
    m=content['marketing']
    check('marketing uses the same primary case',escape(m['origin']) in index and m['case'] in traces)
    check('marketing prose is generated from the content data',all(escape(m[k]) in index for k in ['label','quote','today','customer','next']))
    check('marketing module detail is generated from the content data',all(escape(s) in index for s in m['touches']))
    check('marketing proof and falsifier come from the case journey',
          all(escape(s) in index for s in traces[m['case']]['journey']['ui']['content']['checks'])
          and escape(traces[m['case']]['journey']['ui']['content']['falsifier']) in index)
    print(f'{checks} evidence/content checks; {len(failures)} failures')
    for failure in failures:print('FAIL',failure)
    return bool(failures)
if __name__=='__main__':raise SystemExit(main())
