<?php

$root = dirname(__DIR__);
$checks = array();

function checkResult(&$checks, $label, $condition)
{
    $checks[] = array('label' => $label, 'pass' => (bool) $condition);
}

$notificationPath = $root . '/services/notifications/NotificationService.php';
$documentSyncPath = $root . '/services/facto/FactoDocumentsSyncService.php';
$endpointPath = $root . '/api/dashboard/force_sync_facto.php';
$appJsPath = $root . '/assets/js/app.js';

$notificationSource = is_file($notificationPath) ? file_get_contents($notificationPath) : '';
$documentSyncSource = is_file($documentSyncPath) ? file_get_contents($documentSyncPath) : '';
$endpointSource = is_file($endpointPath) ? file_get_contents($endpointPath) : '';
$appJsSource = is_file($appJsPath) ? file_get_contents($appJsPath) : '';

checkResult($checks, 'NotificationService file exists', is_file($notificationPath));
checkResult($checks, 'NotificationService has no typed PDO property', strpos($notificationSource, 'private PDO $pdo') === false);
checkResult($checks, 'NotificationService has no typed array property', strpos($notificationSource, 'private array $tableCache') === false);
checkResult($checks, 'NotificationService class loads', false);

try {
    require_once $notificationPath;
    $checks[count($checks) - 1]['pass'] = class_exists('NotificationService', false);
} catch (Throwable $e) {
    $checks[count($checks) - 1]['error'] = $e->getMessage();
}

checkResult(
    $checks,
    'Issued-document sync uses FACTO direction flag 1',
    preg_match('/syncIssuedDocuments[\s\S]*?syncDocumentsByFlag\(1,/', $documentSyncSource) === 1
);
checkResult(
    $checks,
    'Received-document sync uses FACTO direction flag 0',
    preg_match('/syncReceivedDocuments[\s\S]*?syncDocumentsByFlag\(0,/', $documentSyncSource) === 1
);
checkResult($checks, 'Production test document 862 exclusion present', strpos($documentSyncSource, "documentId === '862'") !== false);
checkResult($checks, 'Manual sync endpoint catches Throwable', strpos($endpointSource, 'catch (Throwable $e)') !== false);
checkResult($checks, 'Manual sync endpoint isolates notification failures', strpos($endpointSource, 'catch (Throwable $notificationError)') !== false);
checkResult($checks, 'Manual sync endpoint emits JSON through controlled responder', strpos($endpointSource, 'icylabForceSyncRespond') !== false);
checkResult($checks, 'Frontend reads raw response before JSON parsing', strpos($appJsSource, 'response.text().then') !== false);
checkResult($checks, 'Frontend exposes the real server error', strpos($appJsSource, "ICYLAB FACTO sync error:") !== false);

$failed = 0;
echo "ICYLAB BILLING NOTIFICATION + MANUAL SYNC DIAGNOSTIC v6.17.1\n";
echo 'Generated at UTC: ' . gmdate('Y-m-d H:i:s') . "\n";
echo 'PHP: ' . PHP_VERSION . "\n";
echo 'App root: ' . $root . "\n\n";

foreach ($checks as $check) {
    $status = $check['pass'] ? 'PASS' : 'FAIL';
    echo $check['label'] . ': ' . $status;
    if (!empty($check['error'])) {
        echo ' (' . $check['error'] . ')';
    }
    echo "\n";
    if (!$check['pass']) {
        $failed++;
    }
}

echo "\nRESULT: " . ($failed === 0 ? 'PASS' : 'FAIL') . "\n";
exit($failed === 0 ? 0 : 1);
