<?php
declare(strict_types=1);

require_once dirname(__DIR__) . '/app_runtime.php';
require_once dirname(__DIR__) . '/app_access.php';
require_once dirname(__DIR__) . '/subscriber_router_setup_lib.php';
require_once dirname(__DIR__) . '/output_devices_lib.php';

$root = dirname(__DIR__);
$pages = [
    'subscriber_router_setup.php',
    'android_router_setup.php',
    'subscriber_contracts.php',
    'output_devices.php',
    'server_links.php',
];
$missing = array_values(array_filter($pages, static fn(string $page): bool => !is_file($root . '/' . $page)));
if ($missing) throw new RuntimeException('Missing feature pages: ' . implode(', ', $missing));

$profiles = srs_profiles();
foreach (['cudy', 'netis', 'dlink', 'tplink', 'manual'] as $vendor) {
    if (empty($profiles[$vendor]['models'])) throw new RuntimeException('Missing router vendor: ' . $vendor);
}
$outputRegistry = ua_output_command_registry();
if (($outputRegistry['wifi_qr_cards']['paper'] ?? '') !== '80mm') throw new RuntimeException('Wi-Fi QR printing is not thermal by default.');
foreach (['weak', 'medium', 'strong'] as $level) {
    for ($i = 0; $i < 50; $i++) {
        $password = srs_password($level);
        if (!srs_password_valid_for_level($password, $level)) throw new RuntimeException('Invalid generated password for level: ' . $level);
    }
}
$ssidSample = ['id'=>77, 'full_name'=>'عبد الكريم رجب الخضر'];
$ssidBase = srs_ssid($ssidSample, '2.4');
if ($ssidBase === '' || preg_match('/[^A-Za-z0-9-]/', $ssidBase)) throw new RuntimeException('SSID transliteration is invalid.');
if ($ssidBase !== 'abd') throw new RuntimeException('SSID must use only the subscriber first-name field/fallback token.');
$ssidFromColumns = srs_ssid(['id'=>78, 'first_name'=>'عبد الهادي', 'father_name'=>'محمد', 'last_name'=>'أحمد', 'full_name'=>'عبد الهادي محمد أحمد'], '2.4');
if ($ssidFromColumns !== 'abd-alhady') throw new RuntimeException('SSID included father or family name.');
foreach (['5'=>'-5G', '6'=>'-6G', '7'=>'-7G'] as $band=>$suffix) {
    if (!str_ends_with(srs_ssid($ssidSample, (string)$band), $suffix)) throw new RuntimeException('Missing SSID band suffix: ' . $suffix);
}

$GLOBALS['UA_INSTALL_CONFIG_OVERRIDE'] = [
    'install_type' => 'server',
    'connection_mode' => 'local',
    'server_url' => 'http://127.0.0.1:8080/',
    'link' => [],
];
$serverScope = ua_install_instance_key();
foreach ($pages as $page) {
    if (!ua_install_page_allowed($page)) throw new RuntimeException('Server page blocked: ' . $page);
}

$GLOBALS['UA_INSTALL_CONFIG_OVERRIDE'] = [
    'install_type' => 'branch',
    'connection_mode' => 'internet',
    'server_url' => 'https://main.example.test/admin',
    'link' => ['instance_id' => 'branch-matrix-test', 'status' => 'approved', 'secret' => 'test'],
];
$branchScope = ua_install_instance_key();
foreach ($pages as $page) {
    if (!ua_install_page_allowed($page)) throw new RuntimeException('Branch page blocked: ' . $page);
}
if ($serverScope === $branchScope) throw new RuntimeException('Local output scopes are not isolated.');

$GLOBALS['UA_INSTALL_CONFIG_OVERRIDE'] = [
    'install_type' => 'agent',
    'connection_mode' => 'internet',
    'server_url' => 'https://main.example.test/admin',
    'link' => ['instance_id' => 'agent-matrix-test', 'status' => 'approved', 'secret' => 'test'],
];
foreach (['subscriber_router_setup.php', 'android_router_setup.php'] as $page) {
    if (!ua_install_page_allowed($page)) throw new RuntimeException('Agent router page blocked: ' . $page);
}
if (ua_install_page_allowed('subscribers.php')) throw new RuntimeException('Agent access scope became too broad.');

echo json_encode([
    'ok' => true,
    'platforms' => ['windows', 'android'],
    'install_types' => ['server', 'branch', 'agent'],
    'pages' => $pages,
    'router_vendors' => array_keys($profiles),
    'output_scope_isolated' => true,
    'password_levels' => ['weak', 'medium', 'strong'],
    'ssid_sample' => $ssidBase,
], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL;
