<?php

it('registers module-scoped edge gateway operator routes', function (): void {
    $route = file_get_contents(app_path('modules/edgegateway/routes/moduleEdgeGatewayRoute.php'));

    expect($route)->not->toBeFalse();
    expect($route)->toContain("'/modules/edge-gateways'");
    expect($route)->toContain("'/modules/edge-gateways/workspace/departments'");
    expect($route)->toContain("'/modules/edge-gateways/workspace/departments/{id}'");
    expect($route)->toContain("'/modules/edge-gateways/{id}'");
    expect($route)->toContain("'/modules/edge-gateways/install-token'");
    expect($route)->toContain("'/modules/edge-gateways/install-token/{id}/status'");
    expect($route)->toContain("'/modules/edge-gateways/{id}/discovery'");
    expect($route)->toContain("'/modules/edge-gateways/{id}/bindings'");
    expect($route)->toContain("'/modules/edge-gateways/{id}/operations'");
    expect($route)->toContain("'/modules/edge-gateways/{id}/operations/{operationId}/cancel'");
    expect($route)->toContain("'/modules/edge-gateways/{id}/operations/{operationId}/events'");
    expect($route)->toContain("'/modules/edge-gateways/{id}/rotate-credentials'");
    expect($route)->toContain("'/modules/edge-gateways/departments/{id}/cutover'");
    expect($route)->toContain('requireModuleEnabled()');
    expect($route)->not->toContain("'/modules/edge-gateways/{id}/shell-sessions'");
});

it('registers edge gateway config endpoints from the module route directory', function (): void {
    $route = file_get_contents(app_path('modules/edgegateway/routes/edgeGatewayConfigRoute.php'));
    $legacyRoute = file_get_contents(app_path('routes/moduleConfigRoute.php'));

    expect($route)->not->toBeFalse();
    expect($route)->toContain("'/edgegateway/config'");
    expect($route)->toContain("'/edgegateway/config/broker-diagnostics'");
    expect($route)->toContain('new edgegateway()');
    expect($route)->toContain('new edge_gateway_manager()');
    expect($legacyRoute)->not->toContain("'/edgegateway/config'");
});

it('registers broker settings as editable edge gateway module config', function (): void {
    $module = file_get_contents(app_path('modules/edgegateway/edgegateway_c.php'));

    expect($module)->not->toBeFalse();
    expect($module)
        ->toContain('edgegateway_broker_url_c::class')
        ->toContain('edgegateway_public_broker_url_c::class')
        ->toContain('edgegateway_broker_auth_mode_c::class')
        ->not->toContain('edgegateway_broker_shared_secret_c::class,');
});

it('keeps only the module facade in the global classes directory and conditionally loads module routes', function (): void {
    $classes = glob(app_path('classes/*.php')) ?: [];
    $edgeGatewayClasses = array_values(array_filter($classes, static function (string $path): bool {
        $name = basename($path);
        return str_contains($name, 'edgegateway') || str_contains($name, 'edge_gateway');
    }));
    $index = file_get_contents(app_path('index.php'));

    expect($edgeGatewayClasses)->toEqual([app_path('classes/edgegateway.php')]);
    expect($index)->toContain("\$routes_path = \$modules_path . DIRECTORY_SEPARATOR . \$module_dir . DIRECTORY_SEPARATOR . 'routes'");
    expect($index)->toContain("method_exists(\$module, 'isEnabled') && !\$module->isEnabled()");
});
