<?php
// Lightweight bootstrap for CLI execution without full index.php
if (!defined('WD')) {
    define('WD', dirname(__DIR__, 2));
}
// Minimal requires
require_once WD . '/classes/permission_node.php';
require_once WD . '/modules/subusers/helpers/subusers_permission_node_key.php';
require_once WD . '/traits/route_t.php';

use traits\route_t;
use modules\subusers\helpers\subusers_permission_node_key;
use classes\permission_node;

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

// Minimal test host using route_t but overriding constructor to avoid HTTP globals
class PermissionRouteTestHost {
    use route_t;
    public function __construct() { /* no-op for CLI */ }
}

$host = new PermissionRouteTestHost();

try {
    $node = $host->definePermission('list_own_bookings', subusers_permission_node_key::BOOKINGS_LIST);
    if (!($node instanceof permission_node)) {
        fail('definePermission should return an instance of classes\\permission_node');
    } else {
        ok('definePermission returns a permission_node');
    }
    if ($node->permission !== 'list_own_bookings') {
        fail('Permission string mismatch');
    } else {
        ok('Permission string correctly set');
    }
    if ($node->subusers_node_key !== subusers_permission_node_key::BOOKINGS_LIST) {
        fail('Subusers node key mismatch');
    } else {
        ok('Subusers node key correctly linked');
    }
} catch (Exception $e) {
    fail('Exception thrown: ' . $e->getMessage());
}

echo "\nPermissionNodeTest completed.\n";
