<?php

app_require('modules/subusers/interfaces/subusers_permission_node_i.php');
app_require('modules/subusers/interfaces/subusers_permission_nodes_i.php');
app_require('modules/subusers/interfaces/subusers_user_grant_i.php');
app_require('modules/subusers/helpers/subusers_permission_node_key.php');
app_require('modules/subusers/helpers/subusers_permission_type.php');
app_require('modules/subusers/traits/subusers_permission_node_t.php');
app_require('modules/subusers/classes/subusers_permission_node.php');
app_require('modules/subusers/traits/subusers_permission_nodes_t.php');
app_require('modules/subusers/classes/subusers_permission_nodes.php');
app_require('modules/subusers/traits/subuser_user_grant_t.php');
app_require('modules/subusers/traits/subusers_user_permissions_t.php');
app_require('modules/subusers/permissions/subusers_permission_nodes_bookings.php');
app_require('modules/subusers/permissions/subusers_permission_nodes_orders.php');
app_require('modules/subusers/permissions/subusers_permission_nodes_selfserve.php');
app_require('modules/subusers/permissions/subusers_permission_nodes_subusers.php');
app_require('modules/subusers/permissions/subusers_permission_nodes_vehicles.php');
app_require('modules/subusers/classes/subuser_user_grant.php');

use modules\subusers\classes\subuser_user_grant;
use modules\subusers\helpers\subusers_permission_node_key;

class TestSubuserGrant extends subuser_user_grant
{
    private array $injectedPermissions;

    public function __construct(int $subuser_id, int $customer_number, array $injectedPermissions)
    {
        $this->injectedPermissions = $injectedPermissions;
        parent::__construct($subuser_id, $customer_number);
    }

    public function loadGrants(): void
    {
        $reflection = new \ReflectionClass($this);
        foreach ($reflection->getProperties() as $property) {
            $type = $property->getType();
            if ($type && is_a($type->getName(), \modules\subusers\classes\subusers_permission_nodes::class, true)) {
                if (method_exists($property, 'isInitialized') && !$property->isInitialized($this)) {
                    continue;
                }

                $container = $property->getValue($this);
                if ($container instanceof \modules\subusers\classes\subusers_permission_nodes) {
                    foreach ($this->injectedPermissions as $permission) {
                        $key = is_string($permission) ? $permission : $permission->name;
                        $node = $container->getNodeByKey($key);
                        if ($node) {
                            $node->value = true;
                        }
                    }
                }
            }
        }
    }
}

it('initializes booking node grants without DB dependency', function (): void {
    $grant = new TestSubuserGrant(9999, 42424242, [subusers_permission_node_key::BOOKINGS_LIST]);

    expect($grant->hasNode(subusers_permission_node_key::BOOKINGS_LIST))->toBeTrue();
    expect($grant->hasNode(subusers_permission_node_key::BOOKINGS_EDIT))->toBeFalse();
    expect($grant->hasNode(subusers_permission_node_key::BOOKINGS_DELETE))->toBeFalse();
});

it('initializes selfserve node grants without DB dependency', function (): void {
    $grant = new TestSubuserGrant(123, 456, [subusers_permission_node_key::SELFSERVE_ADD]);

    expect($grant->hasNode(subusers_permission_node_key::SELFSERVE_ADD))->toBeTrue();
    expect($grant->hasNode(subusers_permission_node_key::SELFSERVE_LIST))->toBeFalse();
    expect($grant->hasNode(subusers_permission_node_key::SELFSERVE_EDIT))->toBeFalse();
    expect($grant->hasNode(subusers_permission_node_key::SELFSERVE_DELETE))->toBeFalse();
});

