<?php

app_require('classes/edge_gateway_manager.php');

use classes\edge_gateway_manager;

it('summarizes fleet usage statistics for the dashboard landing view', function (): void {
    $summary = edge_gateway_manager::summarizeFleetUsage([
        [
            'id' => 701,
            'department_id' => 1,
            'status' => edge_gateway_manager::STATUS_ONLINE,
            'version_drift' => ['is_drifted' => true],
            'channel_status' => ['broker' => ['connected' => true]],
            'active_operation' => ['id' => 91, 'type' => 'DISCOVERY'],
            'recent_operations_summary' => ['pending' => 1, 'in_progress' => 1],
            'backlog_depth' => ['operations' => 2, 'commands' => 3],
            'metadata' => [
                'system_metrics' => [
                    'latency_ms' => 184,
                    'cpu_usage_pct' => 27,
                    'memory_usage_pct' => 61,
                    'disk_usage_pct' => 58,
                ],
            ],
        ],
        [
            'id' => 702,
            'department_id' => 2,
            'status' => edge_gateway_manager::STATUS_OFFLINE,
            'version_drift' => ['is_drifted' => false],
            'channel_status' => ['broker' => ['connected' => false]],
            'active_operation' => null,
            'recent_operations_summary' => ['pending' => 0, 'in_progress' => 0],
            'backlog_depth' => ['operations' => 0, 'commands' => 1],
            'metadata' => [
                'system_metrics' => [
                    'latency_ms' => 412,
                    'cpu_usage_pct' => 9,
                    'memory_usage_pct' => 42,
                    'disk_usage_pct' => 76,
                ],
            ],
        ],
    ], [
        'total' => 2,
        'online' => 1,
        'offline' => 1,
    ], [
        'total' => 3,
        'fallback_overrides' => 2,
        'cloud_only' => 1,
        'local_only' => 1,
    ]);

    expect($summary['gateways'])->toBe([
        'total' => 2,
        'departments' => 2,
        'online' => 1,
        'offline' => 1,
        'degraded' => 0,
        'drifted' => 1,
        'broker_connected' => 1,
    ]);
    expect($summary['inventory'])->toBe([
        'total' => 2,
        'online' => 1,
        'offline' => 1,
    ]);
    expect($summary['bindings'])->toBe([
        'total' => 3,
        'fallback_overrides' => 2,
        'cloud_only' => 1,
        'local_only' => 1,
    ]);
    expect($summary['operations'])->toBe([
        'active' => 1,
        'pending' => 1,
        'in_progress' => 1,
        'backlog' => 2,
    ]);
    expect($summary['commands'])->toBe([
        'backlog' => 4,
    ]);
    expect($summary['system'])->toBe([
        'latency_ms_avg' => 298,
        'cpu_usage_pct_avg' => 18,
        'memory_usage_pct_avg' => 52,
        'disk_usage_pct_avg' => 67,
    ]);
});

it('derives fleet usage directly from cached gateway row summaries', function (): void {
    $summary = edge_gateway_manager::summarizeFleetUsageFromGatewayRows([
        [
            'id' => 701,
            'department_id' => 1,
            'status' => edge_gateway_manager::STATUS_ONLINE,
            'version_drift' => ['is_drifted' => true],
            'channel_status' => ['broker' => ['connected' => true]],
            'active_operation' => ['id' => 91, 'type' => 'DISCOVERY'],
            'recent_operations_summary' => ['pending' => 1, 'in_progress' => 1],
            'backlog_depth' => ['operations' => 2, 'commands' => 3],
            'metadata' => [
                'system_metrics' => [
                    'latency_ms' => 184,
                    'cpu_usage_pct' => 27,
                    'memory_usage_pct' => 61,
                    'disk_usage_pct' => 58,
                ],
            ],
            'inventory_summary' => ['total' => 2, 'online' => 1, 'offline' => 1],
            'binding_summary' => ['total' => 3, 'fallback_overrides' => 2],
            'fallback_summary' => ['cloud_only_relays' => 1, 'local_only_relays' => 1],
        ],
        [
            'id' => 702,
            'department_id' => 2,
            'status' => edge_gateway_manager::STATUS_OFFLINE,
            'version_drift' => ['is_drifted' => false],
            'channel_status' => ['broker' => ['connected' => false]],
            'active_operation' => null,
            'recent_operations_summary' => ['pending' => 0, 'in_progress' => 0],
            'backlog_depth' => ['operations' => 0, 'commands' => 1],
            'metadata' => [
                'system_metrics' => [
                    'latency_ms' => 412,
                    'cpu_usage_pct' => 9,
                    'memory_usage_pct' => 42,
                    'disk_usage_pct' => 76,
                ],
            ],
            'inventory_summary' => ['total' => 1, 'online' => 1, 'offline' => 0],
            'binding_summary' => ['total' => 0, 'fallback_overrides' => 0],
            'fallback_summary' => ['cloud_only_relays' => 0, 'local_only_relays' => 0],
        ],
    ]);

    expect($summary['inventory'])->toBe([
        'total' => 3,
        'online' => 2,
        'offline' => 1,
    ]);
    expect($summary['bindings'])->toBe([
        'total' => 3,
        'fallback_overrides' => 2,
        'cloud_only' => 1,
        'local_only' => 1,
    ]);
});
