"""Four synthetic resilience calculations; no restoration, model or operational actions."""
import json,argparse
from pathlib import Path
from decimal import Decimal as D
INPUTS={'risk-resilience--records': {'expected': 120, 'recovered': 108, 'checked': 96}, 'risk-resilience--workflow': {'total': 40, 'completed': 22, 'pending': 10, 'duplicateLooking': 8, 'minutesEach': 6}, 'risk-resilience--analytics': {'aExposure': 20, 'aEvents': 4, 'bExposure': 80, 'bEvents': 8}, 'risk-resilience--ai': {'tp': 6, 'fp': 3, 'fn': 4, 'tn': 17, 'minutesEach': 10}}
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=='records':
  if not v['expected']>=v['recovered']>=v['checked']:raise ValueError('Inconsistent nested records')
  return dict(missing=v['expected']-v['recovered'],unresolvedRecovered=v['recovered']-v['checked'],recoveryPercent=percent(v['recovered'],v['expected']),checkedExpectedPercent=percent(v['checked'],v['expected']),checkedRecoveredPercent=percent(v['checked'],v['recovered']))
 if dim=='workflow':
  if v['completed']+v['pending']+v['duplicateLooking']!=v['total']:raise ValueError('Categories do not reconcile')
  return dict(completionPercent=percent(v['completed'],v['total']),pendingPercent=percent(v['pending'],v['total']),duplicateInvestigationPercent=percent(v['duplicateLooking'],v['total']),investigationMinutes=v['duplicateLooking']*v['minutesEach'])
 if dim=='analytics':
  if v['aEvents']>v['aExposure'] or v['bEvents']>v['bExposure']:raise ValueError('Event count exceeds exposure')
  a=percent(v['aEvents'],v['aExposure']);b=percent(v['bEvents'],v['bExposure']);n=v['aEvents']+v['bEvents'];t=v['aExposure']+v['bExposure'];return dict(aPercent=a,bPercent=b,events=n,exposure=t,pooledPercent=percent(n,t),unweightedPercent=None if a is None or b is None else (a+b)/2)
 if dim=='ai':
  total=v['tp']+v['fp']+v['fn']+v['tn'];return dict(total=total,precisionPercent=percent(v['tp'],v['tp']+v['fp']),recallPercent=percent(v['tp'],v['tp']+v['fn']),agreementPercent=percent(v['tp']+v['tn'],total),reviewMinutes=(v['tp']+v['fp'])*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))
