<?php ob_start(); ?>
<?php
$h = $health;
$dbOk = ($h['database']['ok'] ?? false);
$allStorageOk = true;
foreach (['cache', 'logs', 'sessions', 'temp'] as $d) {
    if (!($h['storage'][$d]['exists'] ?? false) || !($h['storage'][$d]['writable'] ?? false)) {
        $allStorageOk = false;
        break;
    }
}
$overallOk = $dbOk && $allStorageOk;
?>

<?php admin_content_header('Health-Check', 'activity', [], __('admin.system.subtitle')); ?>

<!-- Overall Status -->
<div class="sys-card" style="margin-bottom: 1.5rem; border-left: 4px solid <?= $overallOk ? 'var(--sem-feedback-success, #10b981)' : 'var(--sem-feedback-error, #ef4444)' ?>;">
    <div style="display: flex; align-items: center; gap: 1rem; padding: 0.25rem 0;">
        <div style="width: 48px; height: 48px; border-radius: 50%; background: <?= $overallOk ? 'var(--sem-feedback-success-bg, #ecfdf5)' : 'var(--sem-feedback-error-bg, #fef2f2)' ?>; display: flex; align-items: center; justify-content: center; flex-shrink: 0;">
            <?= icon($overallOk ? 'check-circle' : 'alert-triangle', 24) ?>
        </div>
        <div>
            <h2 style="margin: 0; font-size: 1.25rem;"><?= $overallOk ? __('admin.system.health_all_ok') : __('admin.system.health_issues') ?></h2>
            <p style="margin: 0.25rem 0 0; color: var(--sem-text-secondary, #64748b); font-size: 0.875rem;">
                <?= e($h['timestamp']) ?>
            </p>
        </div>
    </div>
</div>

<div class="system-grid">

    <!-- CMS & Server -->
    <div class="sys-card">
        <h3 class="sys-card__title"><?= icon('monitor', 18) ?> CMS & Server</h3>
        <table class="sys-table">
            <tr>
                <td>CMS-Version</td>
                <td><strong><?= e($h['cms_version']) ?></strong></td>
            </tr>
            <tr>
                <td>PHP-Version</td>
                <td>
                    <?= e($h['php_version']) ?>
                    <?php if (version_compare($h['php_version'], '8.1.0', '>=')): ?>
                        <span class="badge badge-success">&ge; 8.1</span>
                    <?php else: ?>
                        <span class="badge badge-danger">&lt; 8.1</span>
                    <?php endif; ?>
                </td>
            </tr>
            <tr>
                <td>Server</td>
                <td><?= e($h['server']) ?></td>
            </tr>
            <tr>
                <td>Status</td>
                <td><span class="badge badge-success"><?= e(strtoupper($h['status'])) ?></span></td>
            </tr>
        </table>
    </div>

    <!-- Database -->
    <div class="sys-card">
        <h3 class="sys-card__title"><?= icon('database', 18) ?> <?= __('admin.system.database') ?></h3>
        <table class="sys-table">
            <tr>
                <td><?= __('admin.system.connection') ?></td>
                <td>
                    <?php if ($dbOk): ?>
                        <span class="badge badge-success"><?= __('admin.system.connected') ?></span>
                    <?php else: ?>
                        <span class="badge badge-danger"><?= __('admin.system.connection_error') ?></span>
                    <?php endif; ?>
                </td>
            </tr>
            <?php if ($dbOk): ?>
            <tr>
                <td>Migrations</td>
                <td><?= (int) ($h['database']['table_count'] ?? 0) ?> <?= __('admin.system.tables_found') !== 'admin.system.tables_found' ? '' : '' ?> Tabellen</td>
            </tr>
            <?php endif; ?>
            <?php if (!empty($h['database']['message']) && !$dbOk): ?>
            <tr>
                <td>Fehler</td>
                <td><code style="color: var(--sem-feedback-error, #ef4444);"><?= e($h['database']['message']) ?></code></td>
            </tr>
            <?php endif; ?>
        </table>
    </div>

    <!-- Cache -->
    <div class="sys-card">
        <h3 class="sys-card__title"><?= icon('layers', 18) ?> Cache</h3>
        <table class="sys-table">
            <tr>
                <td>Page-Cache</td>
                <td>
                    <?php if ($h['cache']['page_cache_enabled']): ?>
                        <span class="badge badge-success">Aktiviert</span>
                    <?php else: ?>
                        <span class="badge" style="background: var(--sem-bg-surface-sunken, #f1f5f9); color: var(--sem-text-secondary, #64748b);">Deaktiviert</span>
                    <?php endif; ?>
                </td>
            </tr>
            <tr>
                <td>Cached Files</td>
                <td><?= (int) $h['cache']['cached_files'] ?></td>
            </tr>
        </table>
    </div>

    <!-- Storage -->
    <div class="sys-card">
        <h3 class="sys-card__title"><?= icon('hard-drive', 18) ?> Storage</h3>
        <table class="sys-table">
            <?php foreach (['cache', 'logs', 'sessions', 'temp'] as $dir): ?>
            <tr>
                <td><code>storage/<?= $dir ?>/</code></td>
                <td>
                    <?php if (($h['storage'][$dir]['exists'] ?? false) && ($h['storage'][$dir]['writable'] ?? false)): ?>
                        <span class="badge badge-success">OK</span>
                    <?php elseif (!($h['storage'][$dir]['exists'] ?? false)): ?>
                        <span class="badge badge-danger">Fehlt</span>
                    <?php else: ?>
                        <span class="badge badge-danger">Nicht beschreibbar</span>
                    <?php endif; ?>
                </td>
            </tr>
            <?php endforeach; ?>
            <tr>
                <td>Error-Log</td>
                <td><?= e($h['storage']['error_log_size'] ?? '0 B') ?></td>
            </tr>
        </table>
    </div>

</div>

<!-- API Endpoint Info -->
<div class="card" style="margin-top: 1.5rem;">
    <div class="card-body" style="display: flex; align-items: center; gap: 0.75rem; padding: 1rem 1.25rem;">
        <?= icon('info', 18) ?>
        <span style="font-size: 0.8125rem; color: var(--sem-text-secondary, #64748b);">
            JSON-API: <code>/api/health</code> – Zugriff fuer eingeloggte Admins oder per <code>?api_key=...</code> Parameter (konfigurierbar unter Einstellungen &rarr; <code>health_api_key</code>).
        </span>
    </div>
</div>

<?php $content = ob_get_clean(); include __DIR__ . '/../layouts/admin.php'; ?>
