<?php
$engines = $snapshot['engines'] ?? [];
$modules = $snapshot['modules'] ?? [];
$services = $snapshot['services'] ?? [];
$events = $snapshot['events'] ?? [];
$dependencies = $snapshot['dependencies'] ?? [];
$manifests = $snapshot['manifests'] ?? [];
$checks = $snapshot['checks'] ?? [];
$okCount = count(array_filter($checks, static fn(array $check): bool => ($check['status'] ?? '') === 'ok'));
?>
<link rel="stylesheet" href="/public/assets/css/axiom-kernel.css">

<main class="kernel-page">
    <section class="kernel-hero">
        <div>
            <span class="kernel-kicker">AXIOM CORE</span>
            <h2>Kernel & Registry</h2>
            <p>Registre central des moteurs, modules, services, événements et dépendances de la plateforme.</p>
        </div>
        <div class="kernel-version"><span>Kernel</span><strong>v<?= htmlspecialchars((string)$kernelVersion) ?></strong></div>
    </section>

    <section class="kernel-kpis" aria-label="Indicateurs Kernel">
        <article><span>Moteurs</span><strong><?= count($engines) ?></strong><small>Enregistrés dans le Kernel</small></article>
        <article><span>Modules</span><strong><?= count($modules) ?></strong><small>Composants AXIOM et CDA</small></article>
        <article><span>Services</span><strong><?= count($services) ?></strong><small>Services publiés</small></article>
        <article><span>Conformité</span><strong><?= $okCount ?>/<?= count($checks) ?></strong><small>Manifestes compatibles</small></article>
    </section>

    <section class="kernel-panel">
        <div class="kernel-panel-head"><div><span class="kernel-kicker">REGISTRY</span><h3>Manifestes détectés</h3></div></div>
        <div class="kernel-cards">
            <?php foreach ($manifests as $manifest): ?>
                <article>
                    <div><strong><?= htmlspecialchars((string)($manifest['name'] ?? 'Composant')) ?></strong><code><?= htmlspecialchars((string)($manifest['code'] ?? '')) ?></code></div>
                    <span class="kernel-badge <?= ($manifest['status'] ?? '') === 'ok' ? 'is-ok' : 'is-warning' ?>"><?= ($manifest['status'] ?? '') === 'ok' ? 'Conforme' : 'À vérifier' ?></span>
                    <small>v<?= htmlspecialchars((string)($manifest['version'] ?? '0.0.0')) ?> · <?= htmlspecialchars((string)($manifest['type'] ?? 'module')) ?></small>
                </article>
            <?php endforeach; ?>
        </div>
    </section>

    <section class="kernel-grid">
        <article class="kernel-panel">
            <div class="kernel-panel-head"><div><span class="kernel-kicker">ENGINES</span><h3>Moteurs</h3></div></div>
            <div class="kernel-table-wrap"><table><thead><tr><th>Code</th><th>Nom</th><th>Version</th><th>État</th></tr></thead><tbody>
            <?php foreach ($engines as $engine): ?><tr><td><code><?= htmlspecialchars((string)$engine['code']) ?></code></td><td><?= htmlspecialchars((string)$engine['name']) ?></td><td><?= htmlspecialchars((string)$engine['version']) ?></td><td><span class="kernel-badge <?= !empty($engine['enabled']) ? 'is-ok' : 'is-warning' ?>"><?= !empty($engine['enabled']) ? 'Actif' : 'Inactif' ?></span></td></tr><?php endforeach; ?>
            </tbody></table></div>
        </article>

        <article class="kernel-panel">
            <div class="kernel-panel-head"><div><span class="kernel-kicker">SERVICES</span><h3>Services publiés</h3></div></div>
            <div class="kernel-list"><?php foreach ($services as $service): ?><div><strong><?= htmlspecialchars((string)$service['name']) ?></strong><code><?= htmlspecialchars((string)$service['code']) ?></code><small><?= htmlspecialchars((string)($service['provider'] ?? '')) ?></small></div><?php endforeach; ?></div>
        </article>
    </section>

    <section class="kernel-grid">
        <article class="kernel-panel">
            <div class="kernel-panel-head"><div><span class="kernel-kicker">DEPENDENCIES</span><h3>Dépendances</h3></div></div>
            <div class="kernel-table-wrap"><table><thead><tr><th>Composant</th><th>Dépend de</th><th>Version minimale</th></tr></thead><tbody>
            <?php foreach ($dependencies as $dependency): ?><tr><td><code><?= htmlspecialchars((string)$dependency['component_code']) ?></code></td><td><code><?= htmlspecialchars((string)$dependency['depends_on_code']) ?></code></td><td><?= htmlspecialchars((string)$dependency['min_version']) ?></td></tr><?php endforeach; ?>
            </tbody></table></div>
        </article>

        <article class="kernel-panel">
            <div class="kernel-panel-head"><div><span class="kernel-kicker">EVENT BUS</span><h3>Événements déclarés</h3></div></div>
            <div class="kernel-list"><?php foreach ($events as $event): ?><div><strong><?= htmlspecialchars((string)$event['name']) ?></strong><code><?= htmlspecialchars((string)$event['code']) ?></code><small><?= htmlspecialchars((string)($event['description'] ?? '')) ?></small></div><?php endforeach; ?></div>
        </article>
    </section>

    <section class="kernel-panel">
        <div class="kernel-panel-head"><div><span class="kernel-kicker">QUALITY</span><h3>Contrôles de compatibilité</h3></div></div>
        <div class="kernel-checks"><?php foreach ($checks as $check): ?><div><span class="kernel-dot <?= ($check['status'] ?? '') === 'ok' ? 'is-ok' : 'is-warning' ?>"></span><div><strong><?= htmlspecialchars((string)$check['name']) ?></strong><small><?= htmlspecialchars((string)$check['message']) ?></small></div><b><?= ($check['status'] ?? '') === 'ok' ? 'OK' : 'À vérifier' ?></b></div><?php endforeach; ?></div>
    </section>
</main>

<section class="kernel-panel"><div class="kernel-panel-head"><div><span class="kernel-kicker">RUNTIME 1.0</span><h3>Modules exécutables</h3></div><a href="/public/administration/axiom/hello-world" class="kernel-badge is-ok">Ouvrir Hello World</a></div><div class="kernel-table-wrap"><table><thead><tr><th>Module</th><th>Version</th><th>État Runtime</th><th>Permissions déclarées</th><th>Action</th></tr></thead><tbody><?php foreach (($runtime['modules'] ?? []) as $code => $module): ?><tr><td><strong><?= htmlspecialchars((string)($module['name'] ?? $code)) ?></strong><br><code><?= htmlspecialchars((string)$code) ?></code></td><td><?= htmlspecialchars((string)($module['version'] ?? '0.0.0')) ?></td><td><span class="kernel-badge <?= ($module['runtime_status'] ?? '') === 'booted' ? 'is-ok' : 'is-warning' ?>"><?= htmlspecialchars((string)($module['runtime_status'] ?? 'inconnu')) ?></span><?php if (!empty($module['runtime_error'])): ?><small><?= htmlspecialchars((string)$module['runtime_error']) ?></small><?php endif; ?></td><td><small><?= htmlspecialchars(implode(', ', array_map('strval', $module['permissions'] ?? []))) ?></small></td><td><form method="post" action="/public/administration/axiom/kernel/module-toggle"><?= \App\Core\Csrf::field() ?><input type="hidden" name="code" value="<?= htmlspecialchars((string)$code) ?>"><input type="hidden" name="enabled" value="<?= !empty($module['enabled']) ? '0' : '1' ?>"><button type="submit" class="kernel-badge <?= !empty($module['enabled']) ? 'is-warning' : 'is-ok' ?>"><?= !empty($module['enabled']) ? 'Désactiver' : 'Activer' ?></button></form></td></tr><?php endforeach; ?></tbody></table></div></section>
