<?php

it('registers the v2 operator-facing edge gateway routes', function (): void {
    $route = file_get_contents(app_path('routes/edgeGatewaysRoute.php'));

    expect($route)->not->toBeFalse();
    expect($route)->toContain("'/edge-gateways'");
    expect($route)->toContain("'/edge-gateways/{id}'");
    expect($route)->toContain("'/edge-gateways/install-token'");
    expect($route)->toContain("'/edge-gateways/install-token/{id}/status'");
    expect($route)->toContain("'/edge-gateways/{id}/discovery'");
    expect($route)->toContain("'/edge-gateways/{id}/bindings'");
    expect($route)->toContain("'/edge-gateways/{id}/operations'");
    expect($route)->toContain("'/edge-gateways/{id}/operations/{operationId}/cancel'");
    expect($route)->toContain("'/edge-gateways/{id}/operations/{operationId}/events'");
    expect($route)->toContain("'/edge-gateways/{id}/tasks'");
    expect($route)->toContain("'/edge-gateways/{id}/logs'");
    expect($route)->toContain("'/edge-gateways/{id}/statistics'");
    expect($route)->toContain("'/edge-gateways/{id}/stream-session'");
    expect($route)->toContain("'/edge-gateways/{id}/shell-sessions'");
    expect($route)->toContain("'/edge-gateways/{id}/rotate-credentials'");
    expect($route)->toContain("'/departments/{id}/gateway-cutover'");
    expect($route)->toContain("add_meta('fleet_usage'");
    expect($route)->toContain('listGatewaysWithFleetUsage(');
    expect($route)->not->toContain('private function requirePermission');
    expect($route)->not->toContain('private function requireDepartmentAccess');
});

it('registers PHP edge agent routes for operations and legacy relay command polling', function (): void {
    $route = file_get_contents(app_path('routes/edgeGatewaysRoute.php'));
    $manager = file_get_contents(app_path('classes/edge_gateway_manager.php'));
    $agent = file_get_contents(app_path('resources/edge-gateway-agent/agent.php'));

    expect($route)->toContain("'/edge-agent/install-token/verify'");
    expect($route)->toContain("'/edge-agent/install-token/status'");
    expect($route)->toContain("'/edge-agent/install.sh'");
    expect($route)->toContain("'/edge-agent/artifacts/manifest.json'");
    expect($route)->toContain("'/edge-agent/artifacts/agent.php'");
    expect($route)->toContain("'/edge-agent/artifacts/lan-worker.php'");
    expect($route)->toContain("'/edge-agent/artifacts/auto-updater.php'");
    expect($route)->toContain("'/edge-agent/artifacts/docker-compose.gateway.yml'");
    expect($route)->toContain("'/edge-agent/artifacts/Dockerfile.edge-agent'");
    expect($route)->toContain("'/edge-agent/artifacts/Dockerfile.lan-worker'");
    expect($route)->toContain("'/edge-agent/artifacts/Dockerfile.auto-updater'");
    expect($route)->toContain("'/edge-agent/artifacts/gateway-launcher.sh'");
    expect($route)->toContain("'/edge-agent/artifacts/truckwash-edge-gateway-stack.service'");
    expect($route)->toContain("'/edge-agent/artifacts/truckwash-edge-agent.service'");
    expect($route)->toContain("'/edge-agent/claim'");
    expect($route)->toContain("'/edge-agent/gateways/{id}/heartbeat'");
    expect($route)->toContain("'/edge-agent/gateways/{id}/operations/next'");
    expect($route)->toContain("'/edge-agent/gateways/{id}/operations/{operationId}/events'");
    expect($route)->toContain("'/edge-agent/gateways/{id}/operations/{operationId}/complete'");
    expect($route)->toContain("'/edge-agent/gateways/{id}/commands/poll'");
    expect($route)->toContain("'/edge-agent/gateways/{id}/commands/{jobId}/result'");
    expect($route)->toContain("'/edge-agent/gateways/{id}/presence'");
    expect($route)->toContain("'/edge-agent/internal/gateways/{id}/validate'");
    expect($route)->toContain("'/edge-agent/internal/gateways/{id}/backlog'");
    expect($route)->toContain("'/edge-agent/internal/gateways/{id}/telemetry'");
    expect($route)->toContain("'/edge-agent/internal/gateways/{id}/logs'");
    expect($route)->toContain("'/edge-agent/internal/browser-streams/validate'");
    expect($route)->toContain("'/edge-agent/internal/shell-sessions/opened'");
    expect($route)->toContain('echo $exception->getMessage()');
    expect($manager)->toContain("\$gatewayPayload['broker_url'] = \$this->buildBrokerPublicUrl();");
    expect($agent)->toContain('applyBrokerUrlFromControlPlaneResponse');
    expect($agent)->toContain("Updated broker URL from control plane heartbeat response.");
    expect($route)->not->toContain("'/edge-agent/gateways/{id}/shell-actions/poll'");
    expect($route)->not->toContain("'/edge-agent/gateways/{id}/shell-sessions/{sessionId}/events'");
});
