"""Four synthetic resilience calculations; no device, interface, access or rehearsal execution."""
import json,argparse
from pathlib import Path
from decimal import Decimal as D
INPUTS={'risk-resilience--devices': {'expected': 60, 'captured': 54, 'timely': 48}, 'risk-resilience--integration': {'intents': 30, 'attempts': 38, 'retries': 8, 'confirmed': 24, 'minutesEach': 7}, 'risk-resilience--governance': {'total': 25, 'authority': 20, 'evidenceAccess': 18, 'both': 16}, 'risk-resilience--learning-tools': {'invited': 24, 'attended': 20, 'firstOwner': 16, 'handoff': 12, 'minutesEach': 5}}
def percent(a,b):return None if b==0 else a*100/b
def calculate(id,x):
 if id not in INPUTS or set(x)!=set(INPUTS[id]):raise ValueError('Unknown case or incorrect fields')
 if any(type(v) is not int or v<0 for v in x.values()):raise ValueError('Use nonnegative integer counts/workload values')
 v={k:D(n) for k,n in x.items()};dim=id.split('--')[1]
 if dim=='devices':
  if not v['expected']>=v['captured']>=v['timely']:raise ValueError('Inconsistent nested observations')
  return dict(missing=v['expected']-v['captured'],late=v['captured']-v['timely'],capturePercent=percent(v['captured'],v['expected']),timelyPercent=percent(v['timely'],v['expected']),timelyAmongCapturedPercent=percent(v['timely'],v['captured']))
 if dim=='integration':
  if v['intents']+v['retries']!=v['attempts'] or v['confirmed']>v['intents']:raise ValueError('Inconsistent attempt/outcome ledger')
  u=v['intents']-v['confirmed'];return dict(retryPercent=percent(v['retries'],v['attempts']),unresolved=u,unresolvedPercent=percent(u,v['intents']),investigationMinutes=u*v['minutesEach'])
 if dim=='governance':
  t,a,b,z=(v[k] for k in ['total','authority','evidenceAccess','both'])
  if max(a,b)>t or not max(D(0),a+b-t)<=z<=min(a,b):raise ValueError('Inconsistent overlap')
  return dict(both=z,authorityOnly=a-z,accessOnly=b-z,neither=t-a-b+z,jointPercent=percent(z,t))
 if dim=='learning-tools':
  if not v['invited']>=v['attended']>=v['firstOwner']>=v['handoff']:raise ValueError('Inconsistent nesting')
  return dict(attendancePercent=percent(v['attended'],v['invited']),ownerPercent=percent(v['firstOwner'],v['invited']),handoffPercent=percent(v['handoff'],v['invited']),handoffAmongAttendeesPercent=percent(v['handoff'],v['attended']),followUpMinutes=(v['attended']-v['handoff'])*v['minutesEach'])
 raise ValueError('Unknown dimension')
if __name__=='__main__':
 p=argparse.ArgumentParser(description=__doc__);p.add_argument('--case',default='all',choices=['all',*INPUTS]);p.add_argument('--input-file');a=p.parse_args();data=json.loads(Path(a.input_file).read_text()) if a.input_file else INPUTS;ids=INPUTS if a.case=='all' else [a.case];print(json.dumps({id:calculate(id,data[id]) for id in ids},default=str,indent=2))
