<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>

<div class="row justify-content-center g-3">
    <div class="col-lg-8">
        <div class="card shadow-sm mb-3">
            <div class="card-header d-flex align-items-center gap-2">
                <i class="bi bi-envelope-check" style="color: var(--brand);"></i> Automated Backups
            </div>
            <div class="card-body">
                <p class="text-muted small mb-3">
                    Runs automatically every day (see <code>php spark backup:run</code>) — a full backup is kept
                    on the server for 7 days, and can also be emailed as an attachment. "Run Backup Now" runs
                    the exact same process on demand, good for testing this without waiting for the schedule.
                </p>
                <form action="<?= site_url('backup/settings') ?>" method="post" class="mb-3">
                    <?= csrf_field() ?>
                    <div class="form-check form-switch mb-2">
                        <input class="form-check-input" type="checkbox" role="switch" name="email_enabled" id="emailEnabled" <?= $emailEnabled ? 'checked' : '' ?>>
                        <label class="form-check-label small fw-semibold" for="emailEnabled">Email a copy of each daily backup</label>
                    </div>
                    <div class="row g-2 align-items-end">
                        <div class="col-md-8">
                            <label class="form-label small">Recipient Email</label>
                            <input type="email" class="form-control form-control-sm" name="recipient_email" value="<?= esc($recipientEmail) ?>" placeholder="e.g. manager@yourhotel.com">
                            <div class="form-text">Sent via this hotel's email settings under Settings &gt; Integrations.</div>
                        </div>
                        <div class="col-md-4">
                            <button type="submit" class="btn btn-sm btn-outline-secondary w-100">Save</button>
                        </div>
                    </div>
                </form>
                <form action="<?= site_url('backup/run-now') ?>" method="post">
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-sm btn-primary"><i class="bi bi-play-circle me-1"></i>Run Backup Now</button>
                </form>

                <?php if (! empty($runs)): ?>
                <hr>
                <h2 class="h6 mb-2">Recent Runs</h2>
                <div class="table-scroll">
                    <table class="table table-sm align-middle mb-0">
                        <thead><tr><th>When</th><th>Status</th><th>Size</th><th>Emailed</th><th>Triggered By</th></tr></thead>
                        <tbody>
                            <?php foreach ($runs as $run): ?>
                                <tr>
                                    <td class="small"><?= esc(date('d M Y, g:i a', strtotime($run->created_at))) ?></td>
                                    <td><span class="badge bg-<?= $run->status === 'success' ? 'success' : 'danger' ?>"><?= esc(ucfirst($run->status)) ?></span></td>
                                    <td class="small"><?= $run->file_size_bytes ? round($run->file_size_bytes / 1024, 1) . ' KB' : '—' ?></td>
                                    <td class="small">
                                        <?php if ($run->emailed): ?><span class="text-success"><i class="bi bi-check-circle"></i></span>
                                        <?php elseif ($run->email_error): ?><span class="text-danger" title="<?= esc($run->email_error) ?>"><i class="bi bi-x-circle"></i></span>
                                        <?php else: ?>—<?php endif; ?>
                                    </td>
                                    <td class="small text-muted"><?= $run->triggered_by ? 'Manual' : 'Scheduled' ?></td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                <?php endif; ?>
            </div>
        </div>

        <div class="card shadow-sm mb-3">
            <div class="card-header d-flex align-items-center gap-2">
                <i class="bi bi-cloud-arrow-down" style="color: var(--brand);"></i> Download Backup
            </div>
            <div class="card-body">
                <p class="text-muted small mb-3">
                    Generates a complete SQL backup of <strong><?= esc($tenant->hotel_name) ?></strong>'s data —
                    every table, every row — and downloads it as a single .sql file. Keep it somewhere safe;
                    it's the only way to restore this hotel's data if something goes wrong.
                </p>
                <a href="<?= site_url('backup/export') ?>" class="btn btn-primary"><i class="bi bi-download me-1"></i>Download Backup Now</a>
            </div>
        </div>

        <div class="card shadow-sm border-danger">
            <div class="card-header d-flex align-items-center gap-2 text-danger">
                <i class="bi bi-exclamation-triangle-fill"></i> Restore From Backup
            </div>
            <div class="card-body">
                <div class="alert alert-danger small mb-3">
                    <strong>This cannot be undone.</strong> Restoring drops every table in this hotel's database
                    and reloads it from the file you upload — anything created or changed since that backup was
                    taken will be permanently lost. If a restore fails partway through, the tables already
                    dropped and recreated <strong>cannot</strong> be automatically rolled back (MySQL commits table
                    structure changes immediately, regardless of what interrupts the restore) — you'd need to
                    restore again from a backup. Only other tenants' data is guaranteed untouched; this only ever
                    reaches this hotel's own database.
                </div>
                <form action="<?= site_url('backup/restore') ?>" method="post" enctype="multipart/form-data" id="restoreForm">
                    <?= csrf_field() ?>
                    <div class="mb-3">
                        <label class="form-label small fw-semibold">Backup File (.sql)</label>
                        <input type="file" class="form-control" name="backup_file" accept=".sql" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label small fw-semibold">Type <code>RESTORE</code> to confirm</label>
                        <input type="text" class="form-control" id="confirmPhrase" name="confirm_phrase" autocomplete="off" required>
                    </div>
                    <button type="submit" class="btn btn-danger" id="restoreBtn" disabled><i class="bi bi-arrow-repeat me-1"></i>Restore Database</button>
                </form>
            </div>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
<?= $this->section('scripts') ?>
<script>
document.getElementById('confirmPhrase').addEventListener('input', function () {
    document.getElementById('restoreBtn').disabled = this.value !== 'RESTORE';
});
document.getElementById('restoreForm').addEventListener('submit', function (e) {
    if (! confirm('This will permanently overwrite every table in this hotel\'s database. Continue?')) {
        e.preventDefault();
    }
});
</script>
<?= $this->endSection() ?>
