"""Six fictional workplace-system calculations; no personnel inference or real-system execution."""
import argparse,json
from decimal import Decimal
from pathlib import Path
D=Decimal
INPUTS={'employer-employee--software-engineering': {'total': 16, 'technical': 13, 'operational': 11, 'both': 9}, 'employer-employee--data-engineering': {'events': 36, 'first': 24, 'retakes': 8, 'duplicates': 4, 'learners': 24, 'latestCorrect': 15}, 'employer-employee--visualisation': {'a_total': 30, 'a_done': 18, 'b_total': 10, 'b_done': 8}, 'employer-employee--nlp': {'tp': 8, 'fp': 4, 'fn': 2, 'tn': 14}, 'employer-employee--hci': {'total': 18, 'discovered': 15, 'submitted': 12, 'interpreted': 9}, 'employer-employee--devops': {'total': 12, 'technical': 10, 'workplace': 8, 'both': 7}}
def percent(n,d):return None if d==0 else D(str(n))/D(str(d))*100
def groups(total,a,b,both):
 if min(total,a,b,both)<0 or max(a,b)>total or not max(0,a+b-total)<=both<=min(a,b):raise ValueError('Inconsistent overlap')
 return dict(both=both,firstOnly=a-both,secondOnly=b-both,neither=total-a-b+both)
def calculate(id,x):
 if id not in INPUTS:raise ValueError('Unknown case')
 if set(x)!=set(INPUTS[id]):raise ValueError('Missing or unexpected fields')
 if any(type(v) is not int or v<0 for v in x.values()):raise ValueError('Use non-negative integer fixture values')
 p=lambda a,b:percent(x[a],x[b])
 def nested(*keys):
  if any(x[a]<x[b] for a,b in zip(keys,keys[1:])):raise ValueError('Inconsistent nested counts')
 d=id.split('--')[1]
 if d=='software-engineering':return dict(**groups(x['total'],x['technical'],x['operational'],x['both']),technicalPercent=p('technical','total'),operationalPercent=p('operational','total'),jointPercent=p('both','total'))
 if d=='data-engineering':
  if x['first']+x['retakes']+x['duplicates']!=x['events']:raise ValueError('Event categories do not reconcile')
  nested('learners','latestCorrect');attempts=x['first']+x['retakes'];return dict(legitimateAttempts=attempts,duplicatePercent=p('duplicates','events'),legitimatePercent=percent(attempts,x['events']),latestLearnerPercent=p('latestCorrect','learners'))
 if d=='visualisation':
  nested('a_total','a_done');nested('b_total','b_done');return dict(aPercent=p('a_done','a_total'),bPercent=p('b_done','b_total'),pooledPercent=percent(x['a_done']+x['b_done'],x['a_total']+x['b_total']))
 if d=='nlp':
  t=sum(x.values());return dict(total=t,precisionPercent=percent(x['tp'],x['tp']+x['fp']),recallPercent=percent(x['tp'],x['tp']+x['fn']),agreementPercent=percent(x['tp']+x['tn'],t))
 if d=='hci':
  nested('total','discovered','submitted','interpreted');return dict(discoveryPercent=p('discovered','total'),submissionPercent=p('submitted','total'),interpretationPercent=p('interpreted','total'),submitterInterpretationPercent=p('interpreted','submitted'))
 if d=='devops':return dict(**groups(x['total'],x['technical'],x['workplace'],x['both']),technicalPercent=p('technical','total'),workplacePercent=p('workplace','total'),jointPercent=p('both','total'))
 raise ValueError('Unknown case')
def fixture_results():return {id:calculate(id,x) for id,x in INPUTS.items()}
def self_test():
 assert percent(0,0) is None
 try:groups(12,10,8,1)
 except ValueError:pass
 else:raise AssertionError('Impossible overlap accepted')
 assert len(fixture_results())==6
 return 'Bounded fixture checks passed; not independent expert validation.'
if __name__=='__main__':
 p=argparse.ArgumentParser(description=__doc__);p.add_argument('--case',default='all',choices=['all',*INPUTS]);p.add_argument('--input-file');p.add_argument('--self-test',action='store_true');a=p.parse_args()
 if a.self_test:print(self_test())
 else:
  source=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,source[id]) for id in ids},default=str,indent=2))
