"""Targeted Hiba cleanup with real Red Pikmin attacks; no health/death setters."""
import time


def clear_hiba_with_reds(route,location,journal):
    if location!=51:return
    state=route.playable()
    if (state['course'],state['cave'],state['floor'])!=(1,1717514289,2):return
    baseline=journal();deliveries=state['physical_deliveries'];evidence=[]
    for _ in range(24):
        state=route.playable()
        targets=[e for e in state['loaded']['actor_audit']['live_enemies'] if e['enemy_id']==20 and e['health']>0]
        if not targets:break
        enemy=targets[0];position=list(enemy['position']);position[0]-=80
        position=route.command('ground_query',position=position)['last_result']['position'];position[1]+=5
        route.command('actor_position',actor='navi',index=0,position=position);route.game.input(frames=20);route.playable()
        born=route.command('squad_replace',color=1,count=20,captain=0,position=position)
        assert born['last_result']['born']==20
        for _ in range(3):
            state=route.playable();live=state['pikis'];assert len(live)>=10 and all(p['kind']==1 for p in live)
            center=[sum(p['position'][i] for p in live)/len(live) for i in range(3)]
            route.driver.aim_at(route.game,center,radius=20);route.game.input(buttons=route.driver.B,frames=120)
            route.game.input(frames=8);state=route.playable();owner=route.driver.active_navi(state)['actor_id']
            if all(p['action']==0 and p['navi_id']==owner for p in state['pikis']):break
        else:raise AssertionError('actual Red combat squad did not enter owned Formation')
        route.driver.aim_at(route.game,enemy['position'],radius=15)
        for _ in range(20):route.game.press(route.driver.A,frames=6)
        deadline=time.monotonic()+90;observed=[]
        while time.monotonic()<deadline:
            route.game.input(frames=60);state=route.playable()
            selected=next(e for e in state['loaded']['actor_audit']['live_enemies']
                if e['enemy_id']==20 and route.driver.distance(e['position'],enemy['position'])<1)
            observed.append(selected)
            if selected['health']<=0 and selected['state']==0:break
            x,y=route.aim(state,{'position':enemy['position']})
            route.game.input(cstick_x=x,cstick_y=y,frames=90)
            for _ in range(4):route.game.press(route.driver.A,frames=6)
        else:raise TimeoutError(f'actual Red attacks did not defeat Hiba: {observed[-1]}')
        assert state['physical_deliveries']==deliveries
        evidence.append({'before':enemy,'after':selected,'observations':observed})
        route.record('actual-red-pikmin-hiba-combat',state,enemy_before=enemy,enemy_after=selected,
            boundary='captain/squad prepared, then actual A throws/AI attacks/native Hiba FSM; no health or death setters')
    else:raise AssertionError('bounded Hiba target count exceeded')
    after=journal();assert after['checks']==baseline['checks'] and after['receipts']==baseline['receipts']
    return {'enemies':evidence,'native_checks_unchanged':after['checks'],'native_receipts_unchanged':after['receipts'],
        'physical_deliveries_unchanged':deliveries,'boundary':'prepared real Reds, normal controller attacks, native death0; no combat campaign claim'}
