<?php
declare(strict_types=1);

$root = dirname(__DIR__);

function ui_contract_read(string $path): string
{
    $value = @file_get_contents($path);
    if (!is_string($value)) {
        throw new RuntimeException('Unable to read: ' . $path);
    }
    return $value;
}

function ui_contract_assert(bool $condition, string $message): void
{
    if (!$condition) {
        throw new RuntimeException($message);
    }
}

$dashboard = ui_contract_read($root . '/dashboard.php');
$dashboardCss = ui_contract_read($root . '/assets/css/noc_dashboard.css');
$routers = ui_contract_read($root . '/routers.php');
$sectors = ui_contract_read($root . '/sectors.php');
$sidebar = ui_contract_read($root . '/sidebar.php');
$sidebarCss = ui_contract_read($root . '/assets/css/ua_noc_fresh_shell.css');
$bulkCss = ui_contract_read($root . '/assets/css/subscriber_messages_bulk.css');
$bulkPage = ui_contract_read($root . '/subscriber_messages_bulk.php');
$printPage = ui_contract_read($root . '/subscriber_custom_print.php');
$printCss = ui_contract_read($root . '/assets/css/subscriber_custom_print.css');
$subscribersCss = ui_contract_read($root . '/assets/css/ua_subscribers_page.css');

ui_contract_assert(strpos($dashboard, 'nocx-dashboard-utility-row') !== false, 'Dashboard utility row is missing.');
ui_contract_assert(strpos($dashboard, 'nocx-security-monitor-row') !== false, 'Network monitor is missing from dashboard utility row.');
ui_contract_assert(strpos($dashboard, 'nocx-dashboard-search-panel') !== false, 'Subscriber search panel is missing from dashboard utility row.');
ui_contract_assert(strpos($dashboardCss, 'grid-template-columns:minmax(210px,.62fr) minmax(500px,1.75fr) minmax(260px,.85fr)') !== false, 'Dashboard utility grid no longer mirrors the main dashboard columns.');
ui_contract_assert(strpos($dashboardCss, 'min-height:52px') !== false, 'Dashboard search/monitor equal-height contract is missing.');

ui_contract_assert(strpos($routers, 'ua-router-form-strip') < strpos($routers, 'ua-net-stats'), 'Router form must remain a strip above router statistics/list.');
ui_contract_assert(strpos($sectors, 'ua-sector-form-strip') < strpos($sectors, 'ua-net-stats'), 'Sector form must remain a strip above sector statistics/list.');

ui_contract_assert(strpos($sidebar, "['subscribers_distribution.php', 'توزيع المشتركين'") !== false, 'Subscriber distribution sidebar label is missing.');
$submenuTemplate = substr($sidebar, (int)strpos($sidebar, '<template id="sb3_tpl_'), 2200);
ui_contract_assert(strpos($submenuTemplate, '<span class="ua-sb3-ic">') === false, 'Sidebar submenu must not render icon boxes.');
ui_contract_assert(strpos($sidebarCss, 'max-width:240px !important') !== false, 'Sidebar submenu compact width is missing.');
ui_contract_assert(strpos($sidebarCss, '.ua-sb3-link{') !== false && strpos($sidebarCss, 'border:0 !important') !== false, 'Sidebar submenu borderless link contract is missing.');

ui_contract_assert(strpos($bulkPage, 'إرسال رسالة للمحددين') !== false, 'Selected-subscriber message heading is missing.');
ui_contract_assert(strpos($bulkCss, 'repeat(4, minmax(125px, 1fr))') !== false, 'Desktop bulk-message controls are no longer kept on one row.');
ui_contract_assert(strpos($bulkCss, '.smb-form input,') !== false && strpos($bulkCss, 'height: 38px') !== false, 'Compact bulk-message control height is missing.');

ui_contract_assert(strpos($subscribersCss, '.ua-custom-print-progress') !== false, 'Custom-print pre-preview progress indicator is missing.');
ui_contract_assert(strpos($printPage, 'data_filter') !== false && strpos($printPage, 'credentials') !== false, 'Custom-print preview filters are missing.');
ui_contract_assert(strpos($printPage, 'foreach ($columns as $key)') !== false, 'Custom-print table no longer follows selected column order.');
ui_contract_assert(strpos($printCss, '.scp-loading') !== false, 'Custom-print preview loading indicator is missing.');

$forbiddenPhrases = [
    'الاسم والعنوان في عمود واحد، مع Quality و Capacity والمعالج والذاكرة',
    'الاسم والعنوان في عمود واحد، ثم الجهاز والمعالج والذاكرة والحالة',
];
foreach ($forbiddenPhrases as $phrase) {
    ui_contract_assert(strpos(ui_contract_read($root . '/assets/css/noc_dashboard.css') . $dashboard . $bulkPage, $phrase) === false, 'Deleted phrase returned: ' . $phrase);
}

echo "recent_ui_contract_test: OK\n";
