"""One additive, current-frame candidate. No identity/time/label inputs."""
import math

def finite(value):return type(value) in (int,float) and math.isfinite(value)

def decide(base,extra,config):
 legacy=bool(base['hit']);conditions={};new=[];reason=None
 if not base['eligible'] or base['torso'] is None or not extra['available']:
  return dict(hit=legacy,r1=legacy,added_rules=[],conditions={},insufficient=True,reason='ADDITIONAL_GEOMETRY_UNAVAILABLE')
 fields=('aspect','torso_angle','torso_ratio','shoulder_below_hip')
 legs=extra['legs']
 if not all(finite(extra.get(k)) for k in fields) or not legs or not all(all(finite(l.get(k)) for k in ('knee_angle','length_ratio','ankle_drop','knee_drop')) for l in legs):
  return dict(hit=legacy,r1=legacy,added_rules=[],conditions={},insufficient=True,reason='INVALID_ADDITIONAL_GEOMETRY')
 d=config['D1'];f=config['D2']
 one=dict(wide=extra['aspect']>=d['box_aspect_min'],torso_direction=extra['torso_angle']>=d['torso_angle_min'],
  compact_torso=extra['torso_ratio']<=d['torso_diagonal_ratio_max'],
  full_leg=len(legs)>=d['available_full_leg_min'],
  compact_legs=all(l['length_ratio']<=d['leg_diagonal_ratio_max'] for l in legs),
  compressed_vertical_span=all(l['ankle_drop']<=d['ankle_drop_box_height_max'] for l in legs),
  extended_knees=all(l['knee_angle']>=d['knee_angle_min'] for l in legs))
 chains=[l['ankle_drop']<=f['ankle_drop_box_height_max'] and l['knee_drop']<=f['knee_drop_box_height_max'] and l['length_ratio']<=f['leg_diagonal_ratio_max'] for l in legs]
 two=dict(wide=extra['aspect']>=f['box_aspect_min'],compact_torso=extra['torso_ratio']<=f['torso_diagonal_ratio_max'],
  shoulders_below_hips=extra['shoulder_below_hip']>=f['shoulder_below_hip_box_height_min'],reversed_same_leg=any(chains))
 if all(one.values()):new.append('D1_COMPACT_EXTENDED')
 if all(two.values()):new.append('D2_REVERSED_ORDER')
 conditions={'D1':one,'D2':two,'D2_same_chain':chains}
 return dict(hit=legacy or bool(new),r1=legacy,added_rules=new,conditions=conditions,insufficient=False,reason=None if legacy or new else 'ADDITIONAL_CONDITIONS_NOT_MET')
