<?php

declare(strict_types=1);

require_once __DIR__ . '/includes/vendor_loader.php';

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

$root = __DIR__;
$steps = [];
$scriptDir = rtrim(str_replace('\\', '/', dirname((string)($_SERVER['SCRIPT_NAME'] ?? '/health.php'))), '/');
if ($scriptDir === '/') {
    $scriptDir = '';
}

function step(string $label, callable $fn): void
{
    global $steps;
    try {
        $steps[] = ['label' => $label, 'ok' => true, 'detail' => (string)$fn()];
    } catch (Throwable $e) {
        $steps[] = ['label' => $label, 'ok' => false, 'detail' => $e->getMessage()];
    }
}

step('PHP version', fn () => PHP_VERSION);
step('Server folder', fn () => $root);
step('Web path', fn () => $scriptDir !== '' ? $scriptDir : '/');
step('config/config.php', function () use ($root) {
    return is_array(require $root . '/config/config.php') ? 'ok' : 'fail';
});
step('config/config.local.php', function () use ($root) {
    $file = $root . '/config/config.local.php';
    if (!is_file($file)) {
        return 'missing — open install.php';
    }
    $local = require $file;
    return 'base_path=' . (string)($local['base_path'] ?? '(auto)');
});
step('PDF vendor (for contracts only)', function () {
    $dir = karvi_resolve_vendor_dir();
    $fpdf = $dir . '/setasign/fpdf/fpdf.php';
    if (!is_file($fpdf)) {
        return 'missing — upload Karvi-VENDOR-FIX.zip to vendor/ (login still works)';
    }
    karvi_load_vendor(true);
    return 'ok';
});
step('Admin login page', function () use ($root) {
    if (!is_file($root . '/pages/admin_login.php')) {
        throw new RuntimeException('missing');
    }
    return 'ok';
});
step('Router bootstrap', function () use ($root) {
    require_once $root . '/includes/bootstrap.php';
    require_once $root . '/includes/helpers.php';
    require_once $root . '/includes/i18n.php';
    require_once $root . '/includes/auth.php';
    init_i18n();
    return 'base_path=' . app_base_path();
});

$failed = array_filter($steps, fn ($s) => !$s['ok']);
$adminUrl = ($scriptDir !== '' ? $scriptDir : '') . '/admin/login/';
?>
<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Karvi Health Check</title>
    <style>
        body { font-family: Arial, sans-serif; max-width: 760px; margin: 2rem auto; padding: 0 1rem; }
        table { width: 100%; border-collapse: collapse; margin: 1rem 0; }
        td, th { border: 1px solid #ddd; padding: 8px; text-align: left; word-break: break-all; }
        .ok { color: #0a7; } .bad { color: #c00; font-weight: bold; }
        .btn { display: inline-block; padding: 12px 24px; background: #b8860b; color: #fff; text-decoration: none; border-radius: 8px; margin-top: 1rem; }
    </style>
</head>
<body>
    <h1>Karvi Health Check</h1>
    <?php if ($failed === []): ?>
        <p class="ok"><strong>All OK — app ready!</strong></p>
    <?php else: ?>
        <p class="bad">Some checks failed (login may still work).</p>
    <?php endif; ?>
    <table>
        <tr><th>Check</th><th>Status</th><th>Detail</th></tr>
        <?php foreach ($steps as $s): ?>
        <tr>
            <td><?= htmlspecialchars($s['label'], ENT_QUOTES, 'UTF-8') ?></td>
            <td class="<?= $s['ok'] ? 'ok' : 'bad' ?>"><?= $s['ok'] ? 'OK' : 'FAIL' ?></td>
            <td><?= htmlspecialchars($s['detail'], ENT_QUOTES, 'UTF-8') ?></td>
        </tr>
        <?php endforeach; ?>
    </table>
    <a class="btn" href="<?= htmlspecialchars($adminUrl, ENT_QUOTES, 'UTF-8') ?>">Open Admin Login</a>
    <?php $pdfTestUrl = ($scriptDir !== '' ? $scriptDir : '') . '/test-pdf.php'; ?>
    <a class="btn" href="<?= htmlspecialchars($pdfTestUrl, ENT_QUOTES, 'UTF-8') ?>" style="background:#444;">Test PDF</a>
    <?php $apiLiveUrl = ($scriptDir !== '' ? $scriptDir : '') . '/api-live-check.php'; ?>
    <a class="btn" href="<?= htmlspecialchars($apiLiveUrl, ENT_QUOTES, 'UTF-8') ?>" style="background:#444;">Live API Check</a>
</body>
</html>
