<?php
// Lightweight bootstrap for CLI execution without full index.php
if (!defined('WD')) {
    define('WD', dirname(__DIR__, 2));
}
// Minimal requires for Subusers permission classes
require_once WD . '/modules/subusers/interfaces/subusers_permission_node_i.php';
require_once WD . '/modules/subusers/interfaces/subusers_permission_nodes_i.php';
require_once WD . '/modules/subusers/interfaces/subusers_user_grant_i.php';
require_once WD . '/modules/subusers/helpers/subusers_permission_node_key.php';
require_once WD . '/modules/subusers/helpers/subusers_permission_type.php';
require_once WD . '/modules/subusers/traits/subusers_permission_node_t.php';
require_once WD . '/modules/subusers/classes/subusers_permission_node.php';
require_once WD . '/modules/subusers/traits/subusers_permission_nodes_t.php';
require_once WD . '/modules/subusers/classes/subusers_permission_nodes.php';
require_once WD . '/modules/subusers/traits/subuser_user_grant_t.php';
require_once WD . '/modules/subusers/traits/subusers_user_permissions_t.php';
require_once WD . '/modules/subusers/permissions/subusers_permission_nodes_bookings.php';
require_once WD . '/modules/subusers/permissions/subusers_permission_nodes_orders.php';
require_once WD . '/modules/subusers/permissions/subusers_permission_nodes_selfserve.php';
require_once WD . '/modules/subusers/permissions/subusers_permission_nodes_subusers.php';
require_once WD . '/modules/subusers/permissions/subusers_permission_nodes_vehicles.php';
require_once WD . '/modules/subusers/classes/subuser_user_grant.php';

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

function ok($message): void { echo "\n\033[32m✔ $message\033[0m\n"; }
function fail($message): void { echo "\n\033[31m✖ $message\033[0m\n"; }

// Test double overriding DB-backed loadGrants() to avoid database dependency in CLI tests
class SelfserveTestSubuserGrant extends base_grant {
    private array $injectedPermissions;

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

    // Override to inject permissions instead of querying DB
    public function loadGrants(): void
    {
        // Manually enable nodes on each permission container without hitting the database
        $reflection = new \ReflectionClass($this);
        $properties = $reflection->getProperties();
        foreach ($properties 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 $perm) {
                        $key = is_string($perm) ? $perm : $perm->name;
                        $node = $container->getNodeByKey($key);
                        if ($node) {
                            $node->value = true;
                        }
                    }
                }
            }
        }
    }
}

// Scenario: subuser has only SELFSERVE_ADD granted => ADD = true; LIST/EDIT/DELETE = false
$grant = new SelfserveTestSubuserGrant(123, 456, [subusers_permission_node_key::SELFSERVE_ADD]);

if ($grant->hasNode(subusers_permission_node_key::SELFSERVE_ADD)) {
    ok('SELFSERVE_ADD is granted as expected');
} else {
    fail('SELFSERVE_ADD should be granted but was not');
}

if (!$grant->hasNode(subusers_permission_node_key::SELFSERVE_LIST)) {
    ok('SELFSERVE_LIST is not granted as expected');
} else {
    fail('SELFSERVE_LIST should not be granted');
}

if (!$grant->hasNode(subusers_permission_node_key::SELFSERVE_EDIT)) {
    ok('SELFSERVE_EDIT is not granted as expected');
} else {
    fail('SELFSERVE_EDIT should not be granted');
}

if (!$grant->hasNode(subusers_permission_node_key::SELFSERVE_DELETE)) {
    ok('SELFSERVE_DELETE is not granted as expected');
} else {
    fail('SELFSERVE_DELETE should not be granted');
}

echo "\nSelfservePermissionInitTest completed.\n";
