<?php

declare(strict_types=1);

header('Content-Type: text/plain; charset=utf-8');

function boolText(bool $value): string
{
    return $value ? 'OK' : 'FAIL';
}

$basePath = realpath(__DIR__ . '/..') ?: (__DIR__ . '/..');

$checks = [];

$checks['PHP version >= 8.2'] = version_compare(PHP_VERSION, '8.2.0', '>=');
$checks['ext-ctype loaded'] = extension_loaded('ctype');
$checks['ext-json loaded'] = extension_loaded('json');
$checks['ext-mbstring loaded'] = extension_loaded('mbstring');
$checks['ext-openssl loaded'] = extension_loaded('openssl');
$checks['ext-pdo loaded'] = extension_loaded('pdo');
$checks['ext-pdo_mysql loaded'] = extension_loaded('pdo_mysql');
$checks['ext-tokenizer loaded'] = extension_loaded('tokenizer');
$checks['ext-xml loaded'] = extension_loaded('xml');
$checks['ext-curl loaded'] = extension_loaded('curl');
$checks['ext-fileinfo loaded'] = extension_loaded('fileinfo');

$autoloadPath = $basePath . '/vendor/autoload.php';
$checks['vendor/autoload.php exists'] = file_exists($autoloadPath);

$envPath = $basePath . '/.env';
$checks['.env exists'] = file_exists($envPath);

$appKeyPresent = false;
if (file_exists($envPath)) {
    $env = @file_get_contents($envPath);
    if (is_string($env)) {
        $appKeyPresent = (bool) preg_match('/^APP_KEY\s*=\s*.+$/m', $env);
    }
}
$checks['APP_KEY present in .env'] = $appKeyPresent;

$manifestPath = $basePath . '/public/build/manifest.json';
$checks['public/build/manifest.json exists'] = file_exists($manifestPath);

$storagePath = $basePath . '/storage';
$bootstrapCachePath = $basePath . '/bootstrap/cache';
$checks['storage/ is writable'] = is_dir($storagePath) && is_writable($storagePath);
$checks['bootstrap/cache is writable'] = is_dir($bootstrapCachePath) && is_writable($bootstrapCachePath);

$storageLogsPath = $basePath . '/storage/logs';
$checks['storage/logs exists'] = is_dir($storageLogsPath);
$checks['storage/logs is writable'] = is_dir($storageLogsPath) && is_writable($storageLogsPath);

// Best-effort write test (does not expose contents).
$writeTestOk = false;
if (is_dir($storageLogsPath) && is_writable($storageLogsPath)) {
    $tmpFile = $storageLogsPath . '/.health-write-test';
    $writeTestOk = @file_put_contents($tmpFile, (string) time()) !== false;
    if ($writeTestOk) {
        @unlink($tmpFile);
    }
}
$checks['can write in storage/logs'] = $writeTestOk;

echo "Order Flow - Health Check\n";
echo "Base path: {$basePath}\n";
echo "Time: " . date('c') . "\n\n";

foreach ($checks as $label => $ok) {
    echo str_pad($label, 35, ' ') . ': ' . boolText((bool) $ok) . "\n";
}

echo "\n";
echo "If this file is public, delete it after debugging.\n";

// Cache files overview (helps when config/routes caches are stale/corrupt).
echo "\nBootstrap cache files:\n";
if (! is_dir($bootstrapCachePath)) {
    echo "- (bootstrap/cache missing)\n";
} else {
    $files = array_values(array_filter(scandir($bootstrapCachePath) ?: [], fn ($f) => ! in_array($f, ['.', '..'], true)));
    if ($files === []) {
        echo "- (none)\n";
    } else {
        foreach ($files as $f) {
            $path = $bootstrapCachePath . '/' . $f;
            if (is_file($path)) {
                echo "- {$f} (" . filesize($path) . " bytes)\n";
            }
        }
    }
}
