<?php

it('enables cleaner relay on wash start command and webhook flow', function (): void {
    $commandTrait = file_get_contents(app_path('modules/selfserve/traits/selfserve_lane_command_t.php'));
    $washFlow = file_get_contents(app_path('modules/selfserve/classes/selfserve_wash_flow.php'));

    expect($commandTrait)->not->toBeFalse();
    expect($commandTrait)->toContain('turnOnCleanerRelayForWashStart()');
    expect($commandTrait)->toContain('setMachineCleanerRelayStatusHard(true)');

    expect($washFlow)->not->toBeFalse();
    expect($washFlow)->toContain('enableCleanerRelayForStartedWash(');
    expect($washFlow)->toContain('setMachineCleanerRelayStatusHard(true)');
});

it('does not enable cleaner relay before deferred start relay side effects are checked', function (): void {
    $commandTrait = file_get_contents(app_path('modules/selfserve/traits/selfserve_lane_command_t.php'));

    expect($commandTrait)->not->toBeFalse();
    $startCaseOffset = strpos($commandTrait, 'case selfserve_lane_command::START:');
    expect($startCaseOffset)->not->toBeFalse();
    $relaySideEffectsOffset = strpos($commandTrait, '$this->runRelaySideEffectsForWashStart($arguments);', (int)$startCaseOffset);
    expect($relaySideEffectsOffset)->not->toBeFalse();

    $startBeforeRelaySideEffects = substr(
        $commandTrait,
        (int)$startCaseOffset,
        (int)$relaySideEffectsOffset - (int)$startCaseOffset
    );

    expect($startBeforeRelaySideEffects)->not->toContain('turnOnCleanerRelayForWashStart()');
});

it('keeps cleaner relay enable wired into machine relay start paths', function (): void {
    $washFlow = file_get_contents(app_path('modules/selfserve/classes/selfserve_wash_flow.php'));
    $moduleRoute = file_get_contents(app_path('routes/moduleSelfServeRoute.php'));

    expect($washFlow)->not->toBeFalse();
    $enableMachineRelayMethodOffset = strpos($washFlow, 'protected function enableMachineRelayIfAllowed');
    expect($enableMachineRelayMethodOffset)->not->toBeFalse();
    $enableMachineRelayMethod = substr($washFlow, (int)$enableMachineRelayMethodOffset, 1200);
    expect($enableMachineRelayMethod)->toContain('$this->enableCleanerRelayForStartedWash($lane);');
    $cleanerEnableOffset = strpos($enableMachineRelayMethod, '$this->enableCleanerRelayForStartedWash($lane);');
    $alreadyEnabledGuardOffset = strpos($enableMachineRelayMethod, 'if ((bool)$session->machine_relay_enabled->value() === true)');
    expect($cleanerEnableOffset)->not->toBeFalse()
        ->and($alreadyEnabledGuardOffset)->not->toBeFalse()
        ->and($cleanerEnableOffset)->toBeLessThan($alreadyEnabledGuardOffset);

    expect($moduleRoute)->not->toBeFalse();
    $machineEnableRouteOffset = strpos($moduleRoute, '/modules/self-serve/lane/relay/machine/enable');
    expect($machineEnableRouteOffset)->not->toBeFalse();
    $machineEnableRoute = substr($moduleRoute, (int)$machineEnableRouteOffset, 1800);
    expect($machineEnableRoute)->toContain('$lane->setMachineCleanerRelayStatusHard(true);');
});
