<?php
$d = $rollbackData ?? [];
$h = static fn($value): string =>
    htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8');
?>
<style>
.rb{display:grid;gap:16px}.rb-card{background:#fff;border:1px solid #dce4ef;border-radius:18px;padding:18px}
.rb-hero{background:linear-gradient(120deg,#4c1d95,#7c3aed);color:#fff}
.rb-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(200px,1fr));gap:12px}
.rb-stat{background:#f8fafc;border:1px solid #dce4ef;border-radius:12px;padding:13px}
.rb-table{width:100%;border-collapse:collapse;min-width:1050px}.rb-table th,.rb-table td{padding:10px;border-bottom:1px solid #e7edf4;text-align:left;vertical-align:top}
.rb-scroll{overflow:auto}.rb-ready{color:#166534;font-weight:800}.rb-blocked{color:#991b1b;font-weight:800}
.rb-btn{display:inline-flex;align-items:center;justify-content:center;padding:9px 13px;border:0;border-radius:9px;background:#7c3aed;color:#fff;font-weight:800;cursor:pointer;text-decoration:none}
.rb-btn.secondary{background:#fff;color:#4c1d95;border:1px solid #c4b5fd}
.rb-form{display:grid;gap:8px;min-width:245px}.rb-form input{padding:9px;border:1px solid #cbd5e1;border-radius:8px}
.rb-note{background:#fff7ed;border:1px solid #fdba74;color:#9a3412;border-radius:12px;padding:13px}
.rb-badge{display:inline-flex;padding:5px 9px;border-radius:999px;background:#ede9fe;color:#5b21b6;font-size:.75rem;font-weight:900}
@media(max-width:760px){.rb-card{padding:14px}.rb-table{min-width:900px}}
</style>

<div class="rb">
<section class="rb-card rb-hero">
    <small>PLATFORM-023.5.3 · AXIOM 10.2.6.2</small>
    <h1>Rollback Transaction Manager</h1>
    <p>Retour transactionnel vers le point de sauvegarde précédant une mise à jour.</p>
    <nav style="display:flex;gap:12px;flex-wrap:wrap">
        <a style="color:#fff" href="/public/studio/mises-a-jour">← Centre de mises à jour</a>
        <a style="color:#fff" href="/public/administration/plateforme/validation">Validation Center</a>
        <a style="color:#fff" href="/public/sauvegardes">Sauvegardes</a>
    </nav>
</section>

<section class="rb-card">
    <div class="rb-grid">
        <div class="rb-stat"><small>Version active</small><br><strong><?= $h($d['current_version'] ?? '') ?></strong></div>
        <div class="rb-stat"><small>Packages avec point de retour</small><br><strong><?= count((array) ($d['packages'] ?? [])) ?></strong></div>
        <div class="rb-stat"><small>Transactions visibles</small><br><strong><?= count((array) ($d['transactions'] ?? [])) ?></strong></div>
        <div class="rb-stat"><small>Actualisé le</small><br><strong><?= $h($d['generated_at'] ?? '') ?></strong></div>
    </div>
</section>

<div class="rb-note">
    <strong>Sécurité :</strong> avant chaque rollback, AXIOM crée une nouvelle sauvegarde complète de secours.
    Le point de retour est ensuite vérifié par SHA-256, manifeste interne et empreintes du projet.
    La restauration remet les fichiers et la base dans l’état antérieur au package choisi.
</div>

<section class="rb-card rb-scroll">
<h2>Packages pouvant être annulés</h2>
<table class="rb-table">
<thead>
<tr>
    <th>Package</th>
    <th>Version installée</th>
    <th>Installation</th>
    <th>Point de retour</th>
    <th>Version cible</th>
    <th>État</th>
    <th>Action sécurisée</th>
</tr>
</thead>
<tbody>
<?php foreach (($d['packages'] ?? []) as $package): ?>
<tr>
    <td><strong>#<?= (int) $package['id'] ?></strong><br><?= $h($package['nom_package'] ?? '') ?></td>
    <td><?= $h($package['version_package'] ?? '') ?></td>
    <td><?= $h($package['installe_at'] ?? '—') ?></td>
    <td>#<?= (int) ($package['backup_job_id'] ?? 0) ?><br><small><?= $h($package['backup_created_at'] ?? '') ?></small></td>
    <td><?= $h($package['backup_version'] ?? '—') ?></td>
    <td>
        <?php if (!empty($package['rollback_ready'])): ?>
            <span class="rb-ready">PRÊT</span>
        <?php else: ?>
            <span class="rb-blocked">BLOQUÉ</span>
        <?php endif; ?>
    </td>
    <td>
        <?php if (!empty($package['rollback_ready'])): ?>
        <form class="rb-form" method="post" action="/public/administration/plateforme/rollback/executer"
              onsubmit="return confirm('Confirmer le rollback complet des fichiers et de la base ? Une sauvegarde de secours sera créée avant la restauration.');">
            <?= \App\Core\Csrf::field() ?>
            <input type="hidden" name="package_id" value="<?= (int) $package['id'] ?>">
            <label>
                <small>Saisir exactement ROLLBACK #<?= (int) $package['id'] ?></small>
                <input required name="confirmation" autocomplete="off" placeholder="ROLLBACK #<?= (int) $package['id'] ?>">
            </label>
            <button class="rb-btn" type="submit">Lancer le rollback</button>
        </form>
        <?php else: ?>
            <small>Archive absente, statut invalide ou empreinte non conforme.</small>
        <?php endif; ?>
    </td>
</tr>
<?php endforeach; ?>
<?php if (empty($d['packages'])): ?>
<tr><td colspan="7">Aucun package installé avec un point de retour exploitable.</td></tr>
<?php endif; ?>
</tbody>
</table>
</section>

<section class="rb-card rb-scroll">
<h2>Historique des transactions</h2>
<table class="rb-table">
<thead>
<tr><th>ID</th><th>Package</th><th>Source → cible</th><th>Sauvegarde cible</th><th>Sauvegarde de secours</th><th>État</th><th>Date</th><th>Message</th></tr>
</thead>
<tbody>
<?php foreach (($d['transactions'] ?? []) as $transaction): ?>
<tr>
<td>#<?= (int) $transaction['id'] ?></td>
<td><?= $h($transaction['nom_package'] ?? ('#' . $transaction['package_id'])) ?></td>
<td><?= $h($transaction['source_version'] ?? '') ?> → <?= $h($transaction['target_version'] ?? '') ?></td>
<td>#<?= (int) $transaction['target_backup_id'] ?></td>
<td><?= !empty($transaction['safety_backup_id']) ? '#' . (int) $transaction['safety_backup_id'] : '—' ?></td>
<td><span class="rb-badge"><?= $h($transaction['status'] ?? '') ?></span></td>
<td><?= $h($transaction['created_at'] ?? '') ?></td>
<td><?= $h($transaction['message'] ?? '') ?></td>
</tr>
<?php endforeach; ?>
<?php if (empty($d['transactions'])): ?>
<tr><td colspan="8">Aucune transaction de rollback enregistrée.</td></tr>
<?php endif; ?>
</tbody>
</table>
</section>
</div>
