"""Equipment consumers after observing native enemy-drop hazards before squad setup.

Enemy death, falling Bomb-rocks, explosions and carrier losses remain native.
The disclosed later-scene fixture waits before birthing its delivery squad;
latent traps remain present and actual attachment/carry/Onyon is still required.
"""
import asyncio
import json
from pathlib import Path
import test_equipment_gameplay_real_server as equipment
from test_equipment_charged import ChargedEquipment, EXTRA
from test_equipment_practical import PracticalRoute
from test_gameplay_real_server import make_parser, run


class DropSettledRoute(PracticalRoute):
    def squad_at_actor(self, location, color, count):
        if location == 184:
            before = self.session.native_journal(self.slot)
            observations = []
            for _ in range(20):
                self.game.input(frames=30)
                state = self.playable()
                bombs = [e for e in state['loaded']['actor_audit']['live_enemies']
                         if e['enemy_id'] == 36]
                observations.append({'retrace': state['retrace'], 'bombs': bombs,
                    'captains': state['effects']['captains'],
                    'piki_count': len(state['pikis'])})
            after = self.session.native_journal(self.slot)
            assert after['checks'] == before['checks']
            assert after['receipts'] == before['receipts']
            self.record('native-post-drop-bomb-observations', state,
                observations=observations,
                boundary='600 ordinary neutral frames before delivery-squad birth; no bomb, health or reward setters; latent traps remain')
        return super().squad_at_actor(location, color, count)


class DropSettledEquipment(ChargedEquipment):
    def __init__(self, session, driver, games):
        super().__init__(session, driver, games)
        self.routes = {slot: DropSettledRoute(game, driver) for slot, game in games.items()}
        for slot, route in self.routes.items():
            route.session = session
            route.slot = slot


async def main(args):
    equipment.Equipment = DropSettledEquipment
    equipment.HELPERS.extend(Path(__file__).with_name(n) for n in EXTRA)
    equipment.HELPERS.extend(Path(__file__).with_name(n) for n in (
        'test_equipment_practical.py', 'test_gameplay_arrival_policy.py',
        'test_equipment_charged.py', 'test_equipment_drop_settled.py'))
    report = await run(args, acceptance=equipment.accept, scope_gates=(),
        acceptance_kind='loaded_scene', boundary=__doc__)
    event = next(e for e in report['events'] if e['label'] == 'equipment-consumers-scope-passed')
    report['subgates'] = {'equipment_consumers': {k: event[k] for k in (
        'passed', 'consumer_item_ids', 'evidence', 'boundary', 'helper_sha256')}}
    report['broad_progression_gate_claimed'] = False
    (args.output / 'report.json').write_text(json.dumps(report, indent=2) + '\n')


if __name__ == '__main__':
    parser = make_parser(__doc__)
    parser.add_argument('--resume-checkpoint', type=Path)
    asyncio.run(main(parser.parse_args()))
