"""Grounded positive burial accepts actual White digging; weight-negative cases remain exact."""
import asyncio
from pathlib import Path
import test_gameplay_actor_cases as cases
from test_equipment_survivors import NearConsumerRoute
from test_gameplay_grounded_cases import HeldAwarePhysical
from test_gameplay_settled_route import SettledRoute
from test_gameplay_real_server import make_parser


def prepare_digging_squad(route,location,color,count):
    _,actor,binding=route.physical(location)
    if color!=4 or binding['depth_max']<=0:
        return NearConsumerRoute.squad_at_actor(route,location,color,count)
    route.clear_hob_geysers(location);route.clear_snow_enemies()
    position=list(actor['position']);position[0]-=25
    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=color,count=count,captain=0,position=position)
    assert born['last_result']['born']==count
    for _ in range(5):
        state=route.playable();live=state['pikis']
        assert live and len(live)>=binding['min_carriers']
        center=[sum(p['position'][i] for p in live)/len(live) for i in range(3)]
        if route.driver.distance(route.driver.active_navi(state)['position'],center)>65:
            near=route.command('ground_query',position=center)['last_result']['position'];near[1]+=5
            route.command('actor_position',actor='navi',index=0,position=near)
            route.game.input(frames=12);route.playable()
        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']
        target=next(p for p in state['pellets'] if p['location_id']==location)
        def owned_work(p):
            return p['kind']==4 and p['navi_id']==owner and (p['action']==0 or
                (p['action']==7 and route.driver.distance(p['position'],target['position'])<80))
        if all(owned_work(p) for p in state['pikis']):break
    else:raise AssertionError('positive buried squad was neither owned Formation nor actual nearby White digging')
    assert len(state['pikis'])>=binding['min_carriers']
    route.record('actual-grounded-white-formation-or-native-digging',state,requested=count,
        surviving=len(state['pikis']),target=target,required=binding['min_carriers'],
        boundary='ACT7 White digging at authored buried target permitted; actual dig/carrypower/Onyon still required')
    return actor,binding


class DiggingRoute(HeldAwarePhysical,NearConsumerRoute):
    def squad_at_actor(self,location,color,count):return prepare_digging_squad(self,location,color,count)


class DiggingGoalRoute(DiggingRoute):
    def squad_at_actor(self,location,color,count):return prepare_digging_squad(self,location,color,min(100,max(2,count*2)))


class DiggingLoadedRoute(DiggingRoute):
    def squad_at_actor(self,location,color,count):
        _,_,binding=self.physical(location)
        if binding['depth_max']>0:return prepare_digging_squad(self,location,color,min(100,max(2,count*2)))
        return SettledRoute.squad_at_actor(self,location,color,count)


EXTRA=('test_gameplay_digging_cases.py','test_gameplay_grounded_cases.py','test_gameplay_carriers.py',
    'test_equipment_survivors.py','test_equipment_gameplay_real_server.py')

if __name__=='__main__':
    parser=make_parser(__doc__);parser.add_argument('--case',choices=('loaded','poko','count'),required=True)
    args=parser.parse_args();cases.GoalRoute=DiggingGoalRoute;cases.StableRoute=DiggingLoadedRoute
    cases.EXTRA.extend(Path(__file__).with_name(n) for n in EXTRA)
    asyncio.run(cases.main(args))
