<?php
/** @var \BotGuard\Core\View $this @var \BotGuard\Core\Csrf $csrf @var array $backups */
$this->layout('layouts.app');
$human = static function (int $bytes): string {
    $u = ['B', 'KB', 'MB', 'GB'];
    $i = 0;
    while ($bytes >= 1024 && $i < 3) { $bytes /= 1024; $i++; }
    return round($bytes, 1) . ' ' . $u[$i];
};
?>
<div class="d-flex justify-content-between align-items-center mb-3">
    <h1 class="h4 mb-0">Database Backups</h1>
    <form method="post" action="/admin/backups">
        <?= $csrf->field() ?>
        <button class="btn btn-primary btn-sm" type="submit"><i class="bi bi-database-add"></i> Create backup</button>
    </form>
</div>

<div class="card border-0 shadow-sm">
    <div class="card-body">
        <div class="table-responsive">
            <table class="table align-middle mb-0">
                <thead><tr class="text-secondary small text-uppercase"><th>File</th><th>Size</th><th>Created</th><th></th></tr></thead>
                <tbody>
                    <?php if (empty($backups)): ?>
                        <tr><td colspan="4" class="text-center text-secondary py-4">No backups yet. Create one above.</td></tr>
                    <?php else: foreach ($backups as $b): ?>
                        <tr>
                            <td><i class="bi bi-file-earmark-zip text-secondary"></i> <code><?= e($b['name']) ?></code></td>
                            <td class="small"><?= e($human($b['size'])) ?></td>
                            <td class="small text-secondary"><?= e($b['modified']) ?></td>
                            <td class="text-end">
                                <a class="btn btn-outline-secondary btn-sm" href="/admin/backups/download?file=<?= urlencode($b['name']) ?>"><i class="bi bi-download"></i></a>
                                <form method="post" action="/admin/backups/delete" class="d-inline" onsubmit="return confirm('Delete this backup file?');">
                                    <?= $csrf->field() ?><input type="hidden" name="file" value="<?= e($b['name']) ?>">
                                    <button class="btn btn-outline-danger btn-sm" type="submit"><i class="bi bi-trash"></i></button>
                                </form>
                            </td>
                        </tr>
                    <?php endforeach; endif; ?>
                </tbody>
            </table>
        </div>
    </div>
    <div class="card-footer bg-body small text-secondary">
        <i class="bi bi-info-circle"></i> Backups are logical SQL dumps stored in <code>storage/backups</code>.
        For large databases, schedule off-host copies and prune old files.
    </div>
</div>
