<?php

app_require('classes/edge_gateway_manager.php');
app_require('classes/object_property.php');
app_require('objects/department_gates_o.php');

use classes\edge_gateway_manager;
use classes\object_property;
use objects\department_gates_o;

final class DepartmentGatesRelayManagerFake extends edge_gateway_manager
{
    public array $switchCalls = [];

    public function __construct()
    {
    }

    public function dispatchRelaySwitch(int $departmentId, string $logicalRelayId, bool $on, array $actionContext = []): array
    {
        $this->switchCalls[] = [
            'department_id' => $departmentId,
            'relay_id' => $logicalRelayId,
            'on' => $on,
        ];

        return [
            'relay_id' => $logicalRelayId,
            'online' => true,
            'on' => $on,
        ];
    }
}

final class DepartmentGatesRelayOpenHarness extends department_gates_o
{
    public function __construct(
        array $config,
        int $departmentId,
        private readonly DepartmentGatesRelayManagerFake $manager,
    ) {
        $this->id = 1001;
        $departmentProperty = new object_property('department_gates', -1, 'department', 'int');
        $departmentProperty->set($departmentId);
        $this->department = $departmentProperty;

        $nameProperty = new object_property('department_gates', -1, 'name', 'string');
        $nameProperty->set('Entry gate');
        $this->name = $nameProperty;

        $configProperty = new object_property('department_gates', -1, 'config', 'json');
        $configProperty->set($config);
        $this->config = $configProperty;
    }

    public function requireSelected(): void
    {
    }

    protected function resolveEdgeGatewayManager(): edge_gateway_manager
    {
        return $this->manager;
    }
}

it('dispatches relay-backed gates through the edge gateway relay manager', function (): void {
    $manager = new DepartmentGatesRelayManagerFake();
    $gate = new DepartmentGatesRelayOpenHarness([
        'type' => 'RELAY',
        'relay_id' => 'ENTRY-GATE-1',
        'pulse_seconds' => 0,
    ], 17, $manager);

    $gate->openGate();

    expect($manager->switchCalls)->toBe([
        [
            'department_id' => 17,
            'relay_id' => 'ENTRY-GATE-1',
            'on' => true,
        ],
    ]);
});
