"""Fresh same-runtime equipment controls and unchanged original placements.

All negative controls and all eleven earned consumers run on this native
artifact. The light control is observed in actual WFG5 before this run earns
Stellar Orb; no state or evidence from an earlier binary is imported.
Later-scene/captain/squad setup remains explicitly disclosed by the routes.
"""
import asyncio
import json
from pathlib import Path
import test_equipment_gameplay_real_server as equipment
from test_equipment_original_alternate import OriginalAlternateEquipment, OriginalAlternateRoute
from test_equipment_survivor_setup import SurvivorEquipment, EXTRA
from test_equipment_worker_recovery import RecoveryEquipment
from test_equipment_lighting_route import WFG
from test_gameplay_real_server import make_parser, run


class FreshOriginalEquipment(OriginalAlternateEquipment):
    def __init__(self,session,driver,games):
        # Initialize the same route mechanisms without LightingEquipment's
        # deliberately historical retained-report constructor.
        RecoveryEquipment.__init__(self,session,driver,games)
        self.routes={slot:OriginalAlternateRoute(game,driver) for slot,game in games.items()}
        for route in self.routes.values():route.session=session

    async def controller_effect(self,slot,kind):
        if kind=='speed' and 'speed' not in self.session.consumer_state['baselines']:
            return await SurvivorEquipment.controller_effect(self,slot,kind)
        return await super().controller_effect(slot,kind)

    async def hazard(self,consumer):
        if consumer=='damage' and 'damage' not in self.session.consumer_state['baselines']:
            return await SurvivorEquipment.hazard(self,consumer)
        return await super().hazard(consumer)

    async def acquire(self,recipient,item):
        result=await super().acquire(recipient,item)
        baselines=self.session.consumer_state['baselines']
        if item==504 and 'light_consumer_before' not in baselines:
            assert self.source(2,504)==(1,61), 'fresh light route must retain the original WFG61 placement'
            before=await asyncio.to_thread(self.routes[1].playable)
            if (before['course'],before['cave'],before['floor'])!=WFG:
                before=await asyncio.to_thread(self.routes[1].scene,*WFG)
            light=before['effects']['light']
            assert not before['equipment'] & (1<<4)
            assert light['stellar_increment']==0 and light['color_ratio']==0 and light['main_cutoff']==45
            baselines['light_consumer_before']=before
            self.session.event('equipment-fresh-native-light-baseline',source=1,location=61,
                scene=list(WFG),before=before,boundary=__doc__)
        return result


async def main(args):
    equipment.Equipment=FreshOriginalEquipment
    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_resupply.py','test_equipment_capacity.py','test_equipment_lift.py',
        'test_equipment_glint_press.py','test_equipment_worker_recovery.py','test_equipment_lighting_route.py',
        'test_equipment_gate_route.py','test_equipment_dirigibug.py','test_equipment_survivor_setup.py',
        'test_equipment_original_alternate.py','test_equipment_fresh_original.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()))
