<?php
$d = $validationData ?? [];
$h = static fn($value): string =>
    htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8');

$badgeClass = static function (string $decision): string {
    if (in_array(
        $decision,
        ['ALLOWED', 'PASSED', 'COMPATIBLE', 'CERTIFIED', 'RESOLVED'],
        true
    )) {
        return 'good';
    }

    if (in_array(
        $decision,
        ['WARNING', 'DEFERRED', 'NOT_CERTIFIED'],
        true
    )) {
        return 'warn';
    }

    return 'bad';
};
?>
<style>
.pc{display:grid;gap:16px}.card{background:#fff;border:1px solid #dce4ef;border-radius:18px;padding:18px}
.hero{background:linear-gradient(120deg,#17395c,#236f9f);color:#fff}.scroll{overflow:auto}
.tbl{width:100%;border-collapse:collapse;min-width:900px}.tbl th,.tbl td{padding:10px;border-bottom:1px solid #e7edf4;text-align:left;vertical-align:top}
.btn{display:inline-flex;padding:9px 13px;border:0;border-radius:9px;background:#1f64a5;color:#fff;font-weight:800;cursor:pointer;text-decoration:none}
.btn.secondary{background:#fff;color:#1f4e78;border:1px solid #b9cce0}.badge{display:inline-flex;padding:5px 9px;border-radius:999px;font-size:.75rem;font-weight:900}
.good{background:#dcfce7;color:#166534}.warn{background:#fef3c7;color:#92400e}.bad{background:#fee2e2;color:#991b1b}
.steps{display:flex;gap:7px;flex-wrap:wrap;margin-top:8px}.step{border:1px solid #dbe4ef;border-radius:10px;padding:9px;background:#f8fafc;font-size:.8rem}
.notice{background:#fff7ed;border:1px solid #fdba74;color:#9a3412;border-radius:12px;padding:12px 14px}
.runtime{display:grid;grid-template-columns:repeat(auto-fit,minmax(190px,1fr));gap:10px}.runtime>div{background:#f8fafc;border:1px solid #dce4ef;border-radius:12px;padding:12px}
.empty{padding:18px;text-align:center;color:#64748b}.run-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(190px,1fr));gap:12px}
.run-card{border:1px solid #dbe4ef;border-radius:14px;padding:14px;background:#fff}.run-card h3{margin:0 0 8px}
.report-head{display:flex;justify-content:space-between;gap:14px;align-items:flex-start;flex-wrap:wrap}
.report-steps{display:grid;gap:12px;margin-top:16px}.report-step{border:1px solid #dbe4ef;border-radius:14px;padding:14px}
.report-step header{display:flex;justify-content:space-between;gap:12px;align-items:center;flex-wrap:wrap}
.report-step ul{margin:8px 0 0;padding-left:20px}.issues{color:#991b1b}.warnings{color:#92400e}
@media(max-width:760px){.card{padding:14px}.tbl{min-width:780px}}
</style>

<div class="pc">
<section class="card hero">
    <small>PLATFORM-023.5.2 · Live Pipeline Monitor PRO</small>
    <h1>Enterprise Validation Center</h1>
    <p>Suivi progressif en direct, historique, scores, durées et motifs de décision.</p>
    <a style="color:#fff" href="/public/studio/mises-a-jour">
        ← Centre de mises à jour
    </a>
</section>

<section class="card">
    <div class="runtime">
        <div><small>Base active</small><br><strong><?= $h($d['runtime_database'] ?? 'inconnue') ?></strong></div>
        <div><small>Packages enregistrés</small><br><strong><?= (int) ($d['runtime_package_count'] ?? 0) ?></strong></div>
        <div><small>Packages affichés</small><br><strong><?= count((array) ($d['packages'] ?? [])) ?></strong></div>
        <div><small>Actualisé le</small><br><strong><?= $h($d['generated_at'] ?? '') ?></strong></div>
    </div>
</section>

<?php foreach (($d['warnings'] ?? []) as $warning): ?>
<div class="notice"><?= $h($warning) ?></div>
<?php endforeach; ?>

<?php if (!empty($d['selected_run'])): ?>
<?php $report = $d['selected_run']; ?>
<section class="card" id="rapport">
    <div class="report-head">
        <div>
            <small>RAPPORT D’EXÉCUTION #<?= (int) ($report['id'] ?? 0) ?></small>
            <h2><?= $h($report['package_name'] ?? '') ?></h2>
            <p><?= $h($report['package_version'] ?? '') ?> · <?= $h($report['archive_name'] ?? '') ?></p>
        </div>
        <div>
            <span class="badge <?= $badgeClass((string) ($report['decision'] ?? '')) ?>">
                <?= $h($report['decision'] ?? '') ?>
            </span>
            <strong><?= (int) ($report['score'] ?? 0) ?>/100</strong>
            <a class="btn secondary" href="/public/administration/plateforme/validation">Fermer</a>
        </div>
    </div>

    <div class="run-grid">
        <div class="run-card"><small>Type</small><h3><?= $h($report['run_type'] ?? '') ?></h3></div>
        <div class="run-card"><small>Statut</small><h3><?= $h($report['status'] ?? '') ?></h3></div>
        <div class="run-card"><small>Durée totale</small><h3><?= (int) ($report['duration_ms'] ?? 0) ?> ms</h3></div>
        <div class="run-card"><small>Étape bloquante</small><h3><?= $h($report['blocking_step'] ?? 'Aucune') ?></h3></div>
        <div class="run-card"><small>Démarré le</small><h3><?= $h($report['created_at'] ?? '') ?></h3></div>
        <div class="run-card"><small>Terminé le</small><h3><?= $h($report['finished_at'] ?? '—') ?></h3></div>
    </div>

    <div class="report-steps">
    <?php foreach (($report['steps'] ?? []) as $step): ?>
        <article class="report-step">
            <header>
                <div>
                    <strong><?= $h($step['step_label'] ?? '') ?></strong>
                    <small> · <?= $h($step['step_code'] ?? '') ?></small>
                </div>
                <div>
                    <span class="badge <?= $badgeClass((string) ($step['decision'] ?? '')) ?>">
                        <?= $h($step['decision'] ?? '') ?>
                    </span>
                    <strong><?= (int) ($step['score'] ?? 0) ?>/100</strong>
                    · <?= (int) ($step['duration_ms'] ?? 0) ?> ms
                </div>
            </header>

            <?php if (!empty($step['message'])): ?>
                <p><?= $h($step['message']) ?></p>
            <?php endif; ?>

            <?php if (!empty($step['issues'])): ?>
                <ul class="issues">
                <?php foreach ($step['issues'] as $issue): ?>
                    <li><?= $h(is_scalar($issue) ? $issue : json_encode($issue, JSON_UNESCAPED_UNICODE)) ?></li>
                <?php endforeach; ?>
                </ul>
            <?php endif; ?>

            <?php if (!empty($step['warnings'])): ?>
                <ul class="warnings">
                <?php foreach ($step['warnings'] as $warning): ?>
                    <li><?= $h(is_scalar($warning) ? $warning : json_encode($warning, JSON_UNESCAPED_UNICODE)) ?></li>
                <?php endforeach; ?>
                </ul>
            <?php endif; ?>
        </article>
    <?php endforeach; ?>
    </div>
</section>
<?php endif; ?>


<section class="card live-monitor" id="live-pipeline-monitor" hidden>
    <div class="report-head">
        <div>
            <small>LIVE PIPELINE MONITOR PRO</small>
            <h2 id="live-title">Exécution en direct</h2>
            <p id="live-subtitle">Initialisation sécurisée…</p>
        </div>
        <div>
            <span class="badge warn" id="live-decision">PENDING</span>
            <strong id="live-score">0/100</strong>
            <button class="btn secondary" type="button" id="live-close">Fermer</button>
        </div>
    </div>

    <div style="margin:18px 0">
        <div style="height:18px;background:#e5edf6;border-radius:999px;overflow:hidden">
            <div id="live-progress-bar" style="width:0%;height:100%;background:linear-gradient(90deg,#2563eb,#7c3aed);transition:width .25s ease"></div>
        </div>
        <div style="display:flex;justify-content:space-between;margin-top:7px">
            <strong id="live-progress-text">0 %</strong>
            <span id="live-elapsed">0 ms</span>
        </div>
    </div>

    <div class="report-steps" id="live-steps"></div>
    <div class="notice" id="live-error" hidden></div>
</section>


<section class="card scroll">
<h2>Packages disponibles</h2>
<table class="tbl">
<thead><tr><th>ID</th><th>Package</th><th>Version</th><th>État</th><th>Importé le</th><th>Action</th></tr></thead>
<tbody>
<?php foreach (($d['packages'] ?? []) as $package): ?>
<tr>
<td>#<?= (int) ($package['id'] ?? 0) ?></td>
<td><?= $h($package['nom_package'] ?? '') ?></td>
<td><?= $h($package['version_package'] ?? '—') ?></td>
<td><?= $h($package['statut'] ?? '—') ?></td>
<td><?= $h($package['importe_at'] ?? '—') ?></td>
<td>
<div style="display:flex;gap:8px;flex-wrap:wrap">
<form method="post" action="/public/administration/plateforme/validation/simuler">
<?= \App\Core\Csrf::field() ?>
<input type="hidden" name="package_id" value="<?= (int) ($package['id'] ?? 0) ?>">
<button class="btn secondary" type="submit">Simulation classique</button>
</form>
<button
    class="btn"
    type="button"
    data-live-pipeline
    data-package-id="<?= (int) ($package['id'] ?? 0) ?>"
    data-package-name="<?= $h($package['nom_package'] ?? '') ?>"
>Suivi en direct</button>
</div>
</td>
</tr>
<?php endforeach; ?>
<?php if (empty($d['packages'])): ?><tr><td colspan="6" class="empty">Aucun package visible.</td></tr><?php endif; ?>
</tbody>
</table>
</section>

<section class="card">
<h2>Dernières exécutions</h2>

<?php foreach (($d['runs'] ?? []) as $run): ?>
<?php $runId = (int) ($run['id'] ?? 0); ?>
<div style="border-top:1px solid #e7edf4;padding:14px 0">
    <div class="report-head">
        <div>
            <strong><?= $h($run['package_name'] ?? '') ?></strong>
            · <?= $h($run['package_version'] ?? '') ?>
            <br><small>#<?= $runId ?> · <?= $h($run['created_at'] ?? '') ?></small>
        </div>
        <div>
            <span class="badge <?= $badgeClass((string) ($run['decision'] ?? '')) ?>">
                <?= $h($run['decision'] ?? '') ?>
            </span>
            <strong><?= (int) ($run['score'] ?? 0) ?>/100</strong>
            · <?= (int) ($run['duration_ms'] ?? 0) ?> ms
            <a class="btn secondary" href="/public/administration/plateforme/validation?run_id=<?= $runId ?>#rapport">
                Ouvrir le rapport
            </a>
        </div>
    </div>

    <div class="steps">
    <?php foreach (($d['steps_by_run'][$runId] ?? []) as $step): ?>
        <div class="step">
            <strong><?= $h($step['step_label'] ?? '') ?></strong><br>
            <span class="badge <?= $badgeClass((string) ($step['decision'] ?? '')) ?>">
                <?= $h($step['decision'] ?? '') ?>
            </span>
            · <?= (int) ($step['score'] ?? 0) ?>/100
            · <?= (int) ($step['duration_ms'] ?? 0) ?> ms
        </div>
    <?php endforeach; ?>
    </div>
</div>
<?php endforeach; ?>

<?php if (empty($d['runs'])): ?>
<p class="empty">Aucune exécution enregistrée. Lancez le pipeline sur un package.</p>
<?php endif; ?>
</section>
</div>

<script src="/public/assets/js/axiom-live-pipeline-monitor-10261.js" defer></script>
