<?php

app_require('routes/moduleSelfServeRoute.php');

if (!class_exists(SelfserveInProgressWashAccessHarness::class)) {
    class SelfserveInProgressWashAccessHarness extends \routes\moduleSelfServeRoute
    {
        public function __construct()
        {
            // Avoid route_t request initialization in this focused unit test.
        }

        public function scopeForCustomer(array $payload, ?int $customer_number): array
        {
            return $this->scopeInProgressWashResponseForCustomer($payload, $customer_number);
        }

        public function scopeForPrincipal(array $payload, int $customer_number, ?int $subuser_id): array
        {
            return $this->scopeInProgressWashResponseForCustomer($payload, [
                'customer_number' => $customer_number,
                'subuser_id' => $subuser_id,
            ]);
        }
    }
}

it('keeps own in-progress self-serve wash details visible to the customer', function (): void {
    $route = new SelfserveInProgressWashAccessHarness();

    $payload = [
        'lane_id' => 7,
        'in_progress' => true,
        'session' => [
            'id' => 704,
            'customer_number' => 12345679,
            'reg' => 'AB12345',
        ],
        'customer' => [
            'customer_number' => 12345679,
            'display_name' => 'Example Customer',
        ],
        'vehicle' => [
            'id' => 55,
            'reg' => 'AB12345',
        ],
    ];

    expect($route->scopeForCustomer($payload, 12345679))->toBe($payload);
});

it('redacts another customers in-progress wash details during customer lane polling', function (): void {
    $route = new SelfserveInProgressWashAccessHarness();

    $scoped = $route->scopeForCustomer([
        'lane_id' => 9,
        'status' => 'MACHINE_STARTED',
        'in_progress' => true,
        'elapsed_minutes' => 4,
        'session' => [
            'id' => 804,
            'customer_number' => 99999999,
            'reg' => 'CD67890',
        ],
        'customer' => [
            'customer_number' => 99999999,
            'display_name' => 'Other Customer',
            'email' => 'other@example.test',
        ],
        'vehicle' => [
            'id' => 77,
            'reg' => 'CD67890',
        ],
    ], 12345679);

    expect($scoped)->toBe([
        'lane_id' => 9,
        'in_progress' => true,
        'session' => null,
        'customer' => null,
        'vehicle' => null,
    ]);
});

it('keeps own driver in-progress self-serve wash details visible', function (): void {
    $route = new SelfserveInProgressWashAccessHarness();

    $payload = [
        'lane_id' => 7,
        'in_progress' => true,
        'session' => [
            'id' => 704,
            'customer_number' => 12345679,
            'subuser_id' => 501,
            'reg' => 'AB12345',
        ],
        'subuser' => [
            'id' => 501,
            'name' => 'Driver One',
        ],
        'customer' => [
            'customer_number' => 12345679,
            'display_name' => 'Example Customer',
        ],
        'vehicle' => [
            'id' => 55,
            'reg' => 'AB12345',
        ],
    ];

    expect($route->scopeForPrincipal($payload, 12345679, 501))->toBe($payload);
});

it('redacts another driver in the same customers in-progress wash during driver lane polling', function (): void {
    $route = new SelfserveInProgressWashAccessHarness();

    $scoped = $route->scopeForPrincipal([
        'lane_id' => 9,
        'status' => 'MACHINE_STARTED',
        'in_progress' => true,
        'elapsed_minutes' => 4,
        'session' => [
            'id' => 804,
            'customer_number' => 12345679,
            'subuser_id' => 502,
            'reg' => 'CD67890',
        ],
        'subuser' => [
            'id' => 502,
            'name' => 'Driver Two',
        ],
        'customer' => [
            'customer_number' => 12345679,
            'display_name' => 'Example Customer',
        ],
        'vehicle' => [
            'id' => 77,
            'reg' => 'CD67890',
        ],
    ], 12345679, 501);

    expect($scoped)->toBe([
        'lane_id' => 9,
        'in_progress' => true,
        'session' => null,
        'customer' => null,
        'vehicle' => null,
    ]);
});
