<?php
/*
 * ICYLAB Billing v6.19
 * Static deployment diagnostic for grouped table viewports and sortable headers.
 * Read-only: does not connect to the database and does not modify files.
 */

date_default_timezone_set('UTC');
$appRoot = dirname(__DIR__);

$files = array(
    'sorter' => $appRoot . '/assets/js/icy-table-sort.js',
    'billing_css' => $appRoot . '/assets/css/10-billing.css',
    'app_css' => $appRoot . '/assets/css/app.css',
    'header' => $appRoot . '/layouts/header.php',
    'documents_php' => $appRoot . '/modules/billing/documents.php',
    'documents_js' => $appRoot . '/modules/billing/documents.js',
    'receivables_php' => $appRoot . '/modules/billing/receivables.php',
    'receivables_js' => $appRoot . '/modules/billing/receivables.js',
    'payables_php' => $appRoot . '/modules/billing/payables.php',
    'quotes_php' => $appRoot . '/modules/crm/quotes.php',
);

$content = array();
foreach ($files as $key => $path) {
    $content[$key] = is_readable($path) ? (string) file_get_contents($path) : '';
}

$checks = array();
$checks['all deployment files readable'] = count(array_filter($content, function ($value) { return $value !== ''; })) === count($content);
$checks['shared sorter v6.19 installed'] = strpos($content['sorter'], "version: '6.19'") !== false
    && strpos($content['sorter'], 'dataset.sortValue') !== false
    && strpos($content['sorter'], 'dataset.sortType') !== false
    && strpos($content['sorter'], "aria-sort") !== false;
$checks['sorter uses delegated dynamic-table binding'] = strpos($content['sorter'], "document.addEventListener('click'") !== false
    && strpos($content['sorter'], 'window.__ICY_TABLE_SORT_BOUND__') !== false;
$checks['billing family pages allow vertical page scroll'] = strpos($content['billing_css'], '#documentsMain.crm-ui-page-content.crm-ui-page') !== false
    && strpos($content['billing_css'], '#receivablesMain.crm-ui-page-content.crm-ui-page') !== false
    && strpos($content['billing_css'], '#payablesMain.crm-ui-page-content.crm-ui-page') !== false
    && substr_count($content['billing_css'], 'overflow-y: auto !important') >= 2;
$checks['family cards no longer consume the full fixed viewport'] = strpos($content['billing_css'], '.billing-document-group.crm-ui-table-card') !== false
    && strpos($content['billing_css'], 'flex: 0 0 auto !important') !== false;
$checks['family table viewport has explicit min and max heights'] = strpos($content['billing_css'], 'min-height: 154px !important') !== false
    && strpos($content['billing_css'], 'max-height: min(796px, 72dvh) !important') !== false
    && strpos($content['billing_css'], 'scrollbar-gutter: stable') !== false;
$checks['ten-row desktop sizing contract installed'] = strpos($content['billing_css'], 'min-height: 74px') !== false
    && strpos($content['billing_css'], 'height: 56px') !== false
    && strpos($content['billing_css'], 'approximately ten rows') !== false;
$checks['documents headers are sortable'] = strpos($content['documents_js'], "sortHead('Emisión','date'") !== false
    && strpos($content['documents_js'], "sortHead('Cliente','text'") !== false
    && strpos($content['documents_js'], "sortHead('Total','number'") !== false
    && strpos($content['documents_js'], 'data-sort-value=') !== false;
$checks['receivables headers are sortable'] = strpos($content['receivables_js'], "sortHead('Cliente','text'") !== false
    && strpos($content['receivables_js'], "sortHead('Saldo','number'") !== false
    && strpos($content['receivables_js'], "sortHead('Vence','date'") !== false
    && strpos($content['receivables_js'], 'data-sort-value=') !== false;
$checks['payables headers are sortable'] = strpos($content['payables_php'], "sortHead('Proveedor','text'") !== false
    && strpos($content['payables_php'], "sortHead('Saldo','number'") !== false
    && strpos($content['payables_php'], "sortHead('Vence','date'") !== false
    && strpos($content['payables_php'], 'data-sort-value=') !== false;
$checks['quotes headers and dynamic rows are sortable'] = substr_count($content['quotes_php'], 'icy-table-sort-button') >= 6
    && strpos($content['quotes_php'], 'class="table quotes-table crm-ui-table crm-ui-mobile-table icy-sortable-table"') !== false
    && strpos($content['quotes_php'], 'data-sort-value="${Number(q.total||0)}"') !== false;
$checks['all four pages load shared sorter with file version'] = strpos($content['documents_php'], '/assets/js/icy-table-sort.js?v=') !== false
    && strpos($content['receivables_php'], '/assets/js/icy-table-sort.js?v=') !== false
    && strpos($content['payables_php'], '/assets/js/icy-table-sort.js?v=') !== false
    && strpos($content['quotes_php'], '/assets/js/icy-table-sort.js?v=') !== false;
$checks['stylesheet cache version advanced'] = strpos($content['header'], "\$v = '1.4.1';") !== false
    && strpos($content['app_css'], '10-billing.css?v=1.4.1') !== false;

$failures = array();

echo "ICYLAB BILLING TABLE VIEWPORT + SORT DIAGNOSTIC v6.19\n";
echo 'Generated at UTC: ' . gmdate('Y-m-d H:i:s') . "\n\n";
foreach ($checks as $label => $passed) {
    echo $label . ': ' . ($passed ? 'PASS' : 'FAIL') . "\n";
    if (!$passed) $failures[] = $label;
}

echo "\nRESULT: " . (empty($failures) ? 'PASS' : 'FAIL') . "\n";
if (!empty($failures)) {
    foreach ($failures as $failure) echo '- ' . $failure . "\n";
    exit(1);
}
exit(0);
