<?php
$formatBytes = static function (int $bytes): string {
    if ($bytes < 1024) return $bytes . ' o';
    if ($bytes < 1024 * 1024) return number_format($bytes / 1024, 1, ',', ' ') . ' Ko';
    if ($bytes < 1024 * 1024 * 1024) return number_format($bytes / 1024 / 1024, 1, ',', ' ') . ' Mo';
    return number_format($bytes / 1024 / 1024 / 1024, 2, ',', ' ') . ' Go';
};
?>
<link rel="stylesheet" href="/public/assets/css/axiom-studio.css">

<main class="axiom-studio">
    <section class="axiom-studio-hero">
        <div>
            <span class="axiom-studio-kicker">STUDIO UX PROFESSIONAL</span>
            <h2>AXIOM Studio</h2>
            <p>Supervisez la plateforme, ses modules, sa santé et ses opérations techniques depuis un seul espace.</p>
        </div>
        <div class="axiom-studio-version">
            <span>Version installée</span>
            <strong><?= htmlspecialchars((string)$version) ?></strong>
        </div>
    </section>

    <section class="axiom-studio-kpis" aria-label="Indicateurs AXIOM">
        <article><span>Modules reconnus</span><strong><?= count($modules) ?></strong><small>Foundation et compatibilité CDA</small></article>
        <article><span>Contrôles conformes</span><strong><?= (int)$healthy ?>/<?= count($checks) ?></strong><small>Santé technique instantanée</small></article>
        <article><span>Packages suivis</span><strong><?= (int)$updateCount ?></strong><small>Historique du centre de mises à jour</small></article>
        <article><span>Sauvegardes suivies</span><strong><?= (int)$backupCount ?></strong><small>Opérations enregistrées</small></article>
    </section>

    <section class="axiom-studio-section">
        <div class="axiom-studio-section-head">
            <div><span class="axiom-studio-kicker">OUTILS</span><h3>Administration de la plateforme</h3></div>
        </div>
        <div class="axiom-studio-tools">
            <a href="/public/parametres/pedagogie#disciplines"><span class="tool-icon">♫</span><strong>Référentiel des disciplines</strong><small>Référentiel pédagogique unique : instruments et activités</small></a>
            <?php if (\App\Core\Permission::can('professeurs.view')): ?><a href="/public/professeurs-pro"><span class="tool-icon">◌</span><strong>Plannings des professeurs</strong><small>Disponibilités, capacités et affectation des élèves</small></a><?php endif; ?>
            <?php if (\App\Core\Permission::can('tarifs.view')): ?><a href="/public/tarifs"><span class="tool-icon">€</span><strong>Tarifs & réductions</strong><small>Grilles, affectations et règles tarifaires</small></a><?php endif; ?>
            <a href="/public/administration/axiom/modules"><span class="tool-icon">▦</span><strong>Modules Studio</strong><small>Créer et suivre les définitions de modules</small></a>
            <a href="/public/administration/axiom/generateurs"><span class="tool-icon">⚒</span><strong>Générateurs</strong><small>Registre du Generator Engine V1</small></a>
            <a href="/public/administration/axiom/fonctionnalites"><span class="tool-icon">◫</span><strong>Feature Flags</strong><small>Activation progressive des capacités AXIOM</small></a>
            <a href="/public/administration/axiom/sdk"><span class="tool-icon">{ }</span><strong>SDK AXIOM</strong><small>Conventions et cycle de vie des modules</small></a>
            <?php if (\App\Core\Permission::can('axiom.kernel.view')): ?><a href="/public/administration/axiom/kernel"><span class="tool-icon">◉</span><strong>Kernel & Registry</strong><small>Moteurs, services, événements et dépendances</small></a><?php endif; ?>
            <?php if (\App\Core\Permission::can('axiom.meta.view')): ?><a href="/public/administration/axiom/meta"><span class="tool-icon">⌘</span><strong>Meta Framework</strong><small>Entités, champs et dictionnaire métier</small></a><?php endif; ?>
            <?php if (\App\Core\Permission::can('axiom.objects.view')): ?><a href="/public/administration/axiom/objects"><span class="tool-icon">⬡</span><strong>Object Model</strong><small>Objets universels, versions, audit et timeline</small></a><?php endif; ?>
            <?php if (\App\Core\Permission::can('axiom.workflow.view')): ?><a href="/public/administration/axiom/workflows"><span class="tool-icon">⇄</span><strong>Workflow Engine</strong><small>États, transitions, instances et tâches</small></a><?php endif; ?>
            <?php if (\App\Core\Permission::can('axiom.planning.view')): ?><a href="/public/administration/axiom/planning"><span class="tool-icon">▦</span><strong>Planning Engine</strong><small>Agendas, ressources, événements, absences et conflits</small></a><?php endif; ?>
            <?php if (\App\Core\Permission::can('axiom.documents.view')): ?><a href="/public/administration/axiom/documents"><span class="tool-icon">▤</span><strong>Document Engine</strong><small>GED, versions, objets liés, modèles et signatures</small></a><?php endif; ?>
            <a href="/public/administration/axiom/document-designer"><span class="tool-icon">✎</span><strong>Document Designer</strong><small>Modèles visuels, variables, versions et génération</small></a>
            <a href="/public/administration/axiom/identites"><span class="tool-icon">◎</span><strong>Identity Engine</strong><small>Personnes, organisations, rôles et relations</small></a>
            <a href="/public/administration/fondation"><span class="tool-icon">◆</span><strong>Foundation</strong><small>Noyau, modules et métadonnées</small></a>
            <a href="/public/administration/axiom/referentiels"><span class="tool-icon">◇</span><strong>Référentiels</strong><small>Listes, codes et métadonnées partagés</small></a>
            <a href="/public/studio/mises-a-jour"><span class="tool-icon">↥</span><strong>Mises à jour</strong><small>Importer et installer les packages</small></a>
            <a href="/public/studio/sauvegardes"><span class="tool-icon">▣</span><strong>Sauvegardes</strong><small>Protéger les fichiers et les données</small></a>
            <a href="/public/administration/securite"><span class="tool-icon">◈</span><strong>Permissions</strong><small>Rôles, comptes et autorisations</small></a>
            <a href="/public/parametres"><span class="tool-icon">⚙</span><strong>Paramètres métier</strong><small>Réglages CDA existants</small></a>
            <a href="/public/administration/fondation/diagnostic" target="_blank" rel="noopener"><span class="tool-icon">{ }</span><strong>Diagnostic JSON</strong><small>Rapport technique exploitable</small></a>
        </div>
    </section>

    <section class="axiom-studio-columns">
        <article class="axiom-studio-panel">
            <div class="axiom-studio-section-head"><div><span class="axiom-studio-kicker">SANTÉ</span><h3>État du système</h3></div></div>
            <div class="axiom-health-list">
                <?php foreach ($checks as $check): ?>
                    <div class="axiom-health-row">
                        <span class="health-dot <?= ($check['status'] ?? '') === 'ok' ? 'is-ok' : 'is-warning' ?>"></span>
                        <div><strong><?= htmlspecialchars((string)($check['name'] ?? 'Contrôle')) ?></strong><small><?= htmlspecialchars((string)($check['value'] ?? '')) ?></small></div>
                        <span class="health-status"><?= ($check['status'] ?? '') === 'ok' ? 'OK' : 'À vérifier' ?></span>
                    </div>
                <?php endforeach; ?>
            </div>
        </article>

        <article class="axiom-studio-panel">
            <div class="axiom-studio-section-head"><div><span class="axiom-studio-kicker">ENVIRONNEMENT</span><h3>Informations techniques</h3></div></div>
            <dl class="axiom-tech-list">
                <div><dt>PHP</dt><dd><?= htmlspecialchars(PHP_VERSION) ?></dd></div>
                <div><dt>Serveur SQL</dt><dd><?= htmlspecialchars((string)$dbVersion) ?></dd></div>
                <div><dt>Base active</dt><dd><?= htmlspecialchars((string)$databaseName) ?></dd></div>
                <div><dt>Stockage CDA</dt><dd><?= htmlspecialchars($formatBytes((int)$storageBytes)) ?></dd></div>
                <div><dt>Entités de métadonnées</dt><dd><?= count($entities) ?></dd></div>
                <div><dt>Mode Foundation</dt><dd>Compatibilité progressive V7 → V8</dd></div>
            </dl>
        </article>
    </section>

    <section class="axiom-studio-section">
        <div class="axiom-studio-section-head"><div><span class="axiom-studio-kicker">REGISTRE</span><h3>Modules reconnus</h3></div></div>
        <div class="axiom-studio-module-grid">
            <?php foreach ($modules as $module): ?>
                <article>
                    <div><strong><?= htmlspecialchars((string)($module['name'] ?? $module['code'])) ?></strong><code><?= htmlspecialchars((string)$module['code']) ?></code></div>
                    <span class="module-version">v<?= htmlspecialchars((string)($module['version'] ?? '0.0.0')) ?></span>
                    <small><?= !empty($module['legacy']) ? 'Module CDA en compatibilité' : 'Module natif AXIOM' ?></small>
                </article>
            <?php endforeach; ?>
        </div>
    </section>
</main>
