<?php
require_once dirname(__DIR__) . '/permissions_catalog.php';

$checks = [];
$checks['catalog_has_dashboard_cards'] =
  isset(ua_permission_catalog()['can_view_dashboard_traffic'])
  && isset(ua_permission_catalog()['can_view_dashboard_receivers']);
$checks['legacy_dashboard_keeps_cards'] =
  ua_permission_array_has(['can_view_dashboard' => true], 'can_view_dashboard_routers');
$checks['granular_dashboard_allows_selected'] =
  ua_permission_array_has([
    'can_view_dashboard' => true,
    'dashboard_permissions_configured' => true,
    'can_view_dashboard_traffic' => true,
  ], 'can_view_dashboard_traffic');
$checks['granular_dashboard_denies_unselected'] =
  !ua_permission_array_has([
    'can_view_dashboard' => true,
    'dashboard_permissions_configured' => true,
    'can_view_dashboard_traffic' => true,
  ], 'can_view_dashboard_routers');
$checks['granular_dashboard_can_hide_all_cards'] =
  !ua_permission_array_has([
    'can_view_dashboard' => true,
    'dashboard_permissions_configured' => true,
  ], 'can_view_dashboard_stats');
$checks['super_grants_everything'] =
  ua_permission_array_has(['all' => true], 'can_view_dashboard_security');

$failed = array_keys(array_filter($checks, static fn($ok) => !$ok));
echo json_encode([
  'ok' => !$failed,
  'permission_count' => count(ua_permission_catalog()),
  'checks' => $checks,
  'failed' => $failed,
], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . PHP_EOL;
exit($failed ? 1 : 0);
