"""Three synthetic finance closing calculations. No network or business actions."""
import json,argparse
from pathlib import Path
from decimal import Decimal as D
INPUTS={'finance-value--nlp': {'tp': 8, 'fp': 4, 'fn': 2, 'tn': 14, 'minutesPerFalsePositive': 5}, 'finance-value--hci': {'invited': 20, 'submitted': 16, 'understood': 12, 'nextAction': 10, 'minutesPerEnquiry': 4}, 'finance-value--devops': {'total': 12, 'technical': 10, 'financial': 9, 'both': 8, 'hoursPerGap': 3}}
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=='nlp':
  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),falsePositiveMinutes=v['fp']*v['minutesPerFalsePositive'])
 if dim=='hci':
  if not v['invited']>=v['submitted']>=v['understood']>=v['nextAction']:raise ValueError('Inconsistent nesting')
  q=v['submitted']-v['nextAction'];return dict(submissionPercent=percent(v['submitted'],v['invited']),understandingPercent=percent(v['understood'],v['invited']),nextActionPercent=percent(v['nextAction'],v['invited']),nextAmongSubmittersPercent=percent(v['nextAction'],v['submitted']),assumedEnquiries=q,supportMinutes=q*v['minutesPerEnquiry'])
 if dim=='devops':
  t,a,b,z=(v[k] for k in ['total','technical','financial','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,technicalOnly=a-z,financialOnly=b-z,neither=t-a-b+z,jointPercent=percent(z,t),reviewHours=(t-z)*v['hoursPerGap'])
 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))
