<?php
$d = $data ?? [];
$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{padding:9px 13px;border:0;border-radius:9px;background:#1f64a5;color:#fff;font-weight:800;cursor:pointer}
.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:7px 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}
@media(max-width:760px){.card{padding:14px}.tbl{min-width:780px}}
</style>

<div class="pc">
<section class="card hero">
    <small>PLATFORM-023.4.1 · Unified Package Registry</small>
    <h1>Enterprise Validation Center</h1>
    <p>Un registre partagé avec le Centre de mises à jour.</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; ?>

<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>
        <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" type="submit">
                Exécuter le pipeline
            </button>
        </form>
    </td>
</tr>
<?php endforeach; ?>

<?php if (empty($d['packages'])): ?>
<tr>
    <td colspan="6" class="empty">
        Aucun package visible dans le registre partagé.
    </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">
    <strong><?= $h($run['package_name'] ?? '') ?></strong>
    · <?= $h($run['package_version'] ?? '') ?>

    <span class="badge <?= $badgeClass((string) ($run['decision'] ?? '')) ?>">
        <?= $h($run['decision'] ?? '') ?>
    </span>

    <strong><?= (int) ($run['score'] ?? 0) ?>/100</strong>

    <?php if (!empty($run['blocking_step'])): ?>
        · Blocage : <?= $h($run['blocking_step']) ?>
    <?php endif; ?>

    <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
        </div>
    <?php endforeach; ?>
    </div>
</div>
<?php endforeach; ?>

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