<?php

// Minimal, self-contained test: when STOP is executed and department self-serve is enabled,
// the machine relay is turned off.

namespace {
    if (!defined('WD')) { define('WD', dirname(__DIR__, 2)); }
}

// Provide minimal stubs to satisfy includes without hitting DB/Redis
namespace classes { class db { public function escape_string($s){ return (string)$s; } }
    class object_property { public function __construct($t=null,$i=null,$n='',$type='',$nullable=false){} public function value(){ return null; } } }
namespace traits { trait db_object_t { protected string $table=''; protected int $id=0; public function setTable(string $t){ $this->table=$t; } public static function add_object(array $fields){ return 1; } public function select($id){ $this->id=(int)$id; return $this; } public function requireSelected(): void {} public function delete(): void {} } }
namespace objects { class department_lanes_o { public $department; public $relay_machine_id; public function __construct(int $departmentId = 0, string $relayId = ''){ $this->department = new \_ValueHolder($departmentId); $this->relay_machine_id = new \_ValueHolder($relayId); } public function exists(): bool { return true; } } }

namespace {

require_once WD . '/modules/selfserve/classes/selfserve_lane.php';
require_once WD . '/modules/selfserve/helpers/selfserve_lane_command.php';
require_once WD . '/modules/selfserve/classes/selfserve_lane_command_arguments.php';
require_once WD . '/modules/selfserve/helpers/selfserve_lane_status.php';
require_once WD . '/modules/selfserve/helpers/selfserve_lane_state.php';
require_once WD . '/modules/selfserve/helpers/selfserve_lane_port.php';
require_once WD . '/modules/selfserve/helpers/selfserve_lane_relay.php';

use modules\selfserve\classes\selfserve_lane;
use modules\selfserve\classes\selfserve_lane_command_arguments;
use modules\selfserve\helpers\selfserve_lane_command;
use modules\selfserve\helpers\selfserve_lane_port;
use modules\selfserve\helpers\selfserve_lane_state;
use modules\selfserve\helpers\selfserve_lane_status;
use modules\selfserve\helpers\selfserve_lane_relay;

// Simple value holder to mimic object_property->value()
class _ValueHolder { private mixed $v; public function __construct(mixed $v){$this->v=$v;} public function value(){return $this->v;} }

// Test double that overrides hardware/DB interactions
class _TestLane extends selfserve_lane {
    public bool $relayOffCalled = false;
    private array $cache = [];

    public function __construct() {
        // Do not call parent ctor (avoids DB); set required fields manually
        $this->id = 1;
        $this->department_lane = new \objects\department_lanes_o(10, 'relay-id-1');
        // Pre-set lane status/state to an active wash
        $this->status = selfserve_lane_status::OCCUPIED;
        $this->setLaneState(selfserve_lane_state::IN_WASH);
        $this->setBypassCustomerNumberValidation(true);
    }

    // Avoid Redis usage
    public function setLaneCache(int $laneId, string $property, mixed $value): self { $this->cache[$laneId.'|'.$property] = $value; return $this; }
    public function getLaneCache(int $laneId, string $property, string $class = null): mixed {
        $key = $laneId.'|'.$property; return $this->cache[$key] ?? null;
    }
    public function clearLaneCache(int $laneId, string $property): self { unset($this->cache[$laneId.'|'.$property]); return $this; }

    // Stub out external effects
    protected function isDepartmentSelfServeEnabled(): bool { return true; }
    public function open(selfserve_lane_port $port): bool { return true; }
    public function invoice(): bool { return true; }
    public function logLaneAction(\modules\selfserve\helpers\selfserve_lane_log_action $action, int $status_code = 200, array $extra_data = []): void { /* no-op */ }
    public function turnOffRelay(selfserve_lane_relay $relay): bool { $this->relayOffCalled = true; return true; }
}

// Run the scenario
$lane = new _TestLane();
$args = new selfserve_lane_command_arguments();
$args->customer_number = null; // bypassed by test lane

$lane->execute(selfserve_lane_command::STOP, $args);

if (!$lane->relayOffCalled) {
    echo "✘ Expected machine relay to be turned off on STOP when self-serve is enabled\n";
    exit(1);
}

echo "✔ Machine relay is turned off on STOP when self-serve is enabled\n";
echo "StopTurnsOffRelayTest completed.\n";

}
