REQUIRED=['queue_item_id', 'order_id', 'fulfillment_order_id', 'assigned_location', 'fulfillment_status', 'request_status', 'inventory_ready', 'packing_ready', 'service_level', 'promised_ship_by', 'carrier_cutoff', 'age_in_queue', 'priority_class', 'priority_reason', 'blocking_hold', 'override_requested_by', 'override_authority', 'override_reason', 'starvation_guard', 'picked_at', 'packed_at', 'queue_state', 'owner', 'evidence_ref']

def _missing(c):
    return [k for k in REQUIRED if k not in c or c[k] in (None,'')]

def evaluate(c):
    if _missing(c): return 'HOLD_UNKNOWN_EVIDENCE'
    if c.get('assigned_location') != c.get('readiness_location'):
        return 'RECOMPUTE_LOCATION_CHANGE'
    if c.get('blocking_hold') not in ('NONE','FALSE',False):
        return 'HOLD_BLOCKING_CONSTRAINT'
    if c.get('inventory_ready') in ('UNKNOWN','STALE',None,'') or c.get('packing_ready') in ('UNKNOWN','STALE',None,''):
        return 'HOLD_UNKNOWN_EVIDENCE'
    if c.get('inventory_ready')!='TRUE' or c.get('packing_ready')!='TRUE':
        return 'HOLD_NOT_READY'
    if c.get('override_requested_by') not in ('NONE',None,''):
        if c.get('override_authority') in ('MISSING','DENIED','UNKNOWN',None,'') or c.get('override_reason') in ('NONE','',None):
            return 'REJECTED_UNAUTHORIZED_OVERRIDE'
    age=int(c.get('age_in_queue',0))
    if age>=240 and c.get('starvation_guard') not in ('ACTIVE','TRIGGERED'):
        return 'HOLD_STARVATION_GUARD_MISSING'
    cutoff=int(c.get('minutes_to_cutoff',999999))
    promise=int(c.get('minutes_to_promised_ship_by',999999))
    if cutoff<=60:
        return 'QUEUE_P0_CUTOFF_RISK'
    if promise<=180:
        return 'QUEUE_P1_PROMISE_RISK'
    if age>=240 and c.get('starvation_guard') in ('ACTIVE','TRIGGERED'):
        return 'QUEUE_P2_STARVATION_GUARD'
    return 'QUEUE_P3_STANDARD_READY'

def controls(c):
    d=evaluate(c)
    notready=(c.get('inventory_ready')!='TRUE' or c.get('packing_ready')!='TRUE')
    loc_changed=c.get('assigned_location') != c.get('readiness_location')
    unauth=(c.get('override_requested_by') not in ('NONE',None,'')) and (c.get('override_authority') in ('MISSING','DENIED','UNKNOWN',None,'') or c.get('override_reason') in ('NONE','',None))
    starv=int(c.get('age_in_queue',0))>=240 and c.get('starvation_guard') not in ('ACTIVE','TRIGGERED')
    hard=c.get('blocking_hold') not in ('NONE','FALSE',False)
    return {
      'dimension_contract': all(k in c for k in REQUIRED),
      'hard_or_readiness_handled': (d in ('HOLD_BLOCKING_CONSTRAINT','HOLD_NOT_READY','HOLD_UNKNOWN_EVIDENCE')) if (hard or notready) and not loc_changed else True,
      'location_change_handled': (d=='RECOMPUTE_LOCATION_CHANGE') if loc_changed else d!='RECOMPUTE_LOCATION_CHANGE',
      'override_authority_handled': (d=='REJECTED_UNAUTHORIZED_OVERRIDE') if unauth and not loc_changed and not hard and not notready else True,
      'starvation_handled': (d=='HOLD_STARVATION_GUARD_MISSING') if starv and not loc_changed and not hard and not notready and not unauth else True,
      'expected_decision': d==c.get('expected_decision')
    }
