<?php

app_require('modules/selfserve/classes/selfserve_wash_flow.php');

use modules\selfserve\classes\selfserve_wash_flow;

class SelfserveNonOwnedVehicleWashFlowHarness extends selfserve_wash_flow
{
    public function __construct()
    {
        // Skip schema bootstrapping for this unit test.
    }

    public function persistedAnswerCustomer(?int $resolvedCustomerNumber): ?int
    {
        return $this->resolvePersistedAnswerCustomerNumber($resolvedCustomerNumber);
    }
}

function selfserve_non_owned_vehicle_route_block(string $route, string $method, string $path): string
{
    $start = strpos($route, "\$this->{$method}('{$path}'");
    if ($start === false) {
        throw new RuntimeException("Route block not found: {$method} {$path}");
    }

    $nextComment = strpos($route, "\n        /**", $start + 1);
    if ($nextComment === false) {
        return substr($route, $start);
    }

    return substr($route, $start, $nextComment - $start);
}

it('allows own-permission customers to preview borrowed registration plates without ownership checks', function (): void {
    $route = file_get_contents(app_path('routes/departmentSelfserveVehicleConditionsRoute.php'));
    expect($route)->not->toBeFalse();

    $allowedBlock = selfserve_non_owned_vehicle_route_block($route, 'get', '/department/selfserve/vehicle/allowed');

    expect($allowedBlock)->toContain("requireAuthenticatedCustomerNumber(\$user, 'list_department_selfserve_vehicle_conditions')");
    expect($allowedBlock)->toContain('previewVehicleEligibility($lane_id, $reg, $customer_number, $vehicle_type_id, [');
    expect($allowedBlock)->toContain("'subuser_id' => \$subuser_id");
    expect($allowedBlock)->not->toContain('assertOwnVehicle');
});

it('allows customer-scoped answers to be stored for borrowed registration plates', function (): void {
    $route = file_get_contents(app_path('routes/departmentSelfserveVehicleConditionsRoute.php'));
    expect($route)->not->toBeFalse();

    $addBlock = selfserve_non_owned_vehicle_route_block($route, 'post', '/department/selfserve/vehicle/conditions');

    expect($addBlock)->toContain("requireAuthenticatedCustomerNumber(\$user, 'add_department_selfserve_vehicle_conditions')");
    expect($addBlock)->toContain('$condition_o->add($department, $lane, $reg, $question, $value, $customer_id)');
    expect($addBlock)->not->toContain('assertOwnVehicle');
});

it('loads saved answers from the authenticated customer context instead of the plate owner', function (): void {
    $flow = new SelfserveNonOwnedVehicleWashFlowHarness();

    expect($flow->persistedAnswerCustomer(10001))->toBe(10001);
    expect($flow->persistedAnswerCustomer(20002))->toBe(20002);
    expect($flow->persistedAnswerCustomer(null))->toBeNull();
    expect($flow->persistedAnswerCustomer(0))->toBeNull();
});

it('scopes saved self-serve answers by customer number and registration plate', function (): void {
    $source = file_get_contents(app_path('objects/department_selfserve_vehicle_conditions_o.php'));

    expect($source)->not->toBeFalse();
    expect($source)->toContain('department, lane, customer, reg and question');
    expect($source)->toContain('$customer_filter = $customer_id === null');
    expect($source)->toContain("'customer_id' => \$customerId");
});
