<?php
/**
 * View: admin/update
 *
 * Injected variables:
 *   @var string|null $error
 *   @var string|null $success
 *   @var string|null $info
 *   @var string      $csrfToken
 *   @var bool        $maintenance
 *   @var array       $backups      Liste [name,size,time] des pre_update_*.zip
 *   @var bool        $keepGit      Métadonnées git préservées (module Developer présent)
 *
 * Available from bootstrap (via extract in View::display):
 *   $auth, $settings, $moduleRegistry, $currentUser
 */

$pageTitle = t('update.title');
include WEBROOT_PATH . '/includes/header.php';
?>

<div class="admin-layout">
<?php include WEBROOT_PATH . '/includes/admin_sidebar.php'; ?>
<div class="admin-content">

<div class="page-header">
    <h1>🔄 <?= t('update.title') ?></h1>
</div>

<?php if ($error): ?>
<div class="alert alert-error" style="white-space:pre-line"><?= htmlspecialchars($error) ?></div>
<?php endif; ?>
<?php if ($success): ?>
<div class="alert alert-success" style="white-space:pre-line"><?= htmlspecialchars($success) ?></div>
<?php endif; ?>
<?php if ($info): ?>
<div class="alert alert-warning" style="white-space:pre-line"><?= htmlspecialchars($info) ?></div>
<?php endif; ?>

<?php if (!empty($maintenance)): ?>
<div class="alert alert-warning" style="display:flex;align-items:center;gap:1rem;flex-wrap:wrap">
    <span style="flex:1">🔧 <?= t('update.maintenance_active') ?></span>
    <form method="post" style="margin:0">
        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken) ?>">
        <input type="hidden" name="action" value="clear_maintenance">
        <button type="submit" class="btn btn-sm btn-secondary"><?= t('update.clear_maintenance') ?></button>
    </form>
</div>
<?php endif; ?>

<!-- Current version -->
<div class="card" style="margin-bottom:1.2rem">
    <h2 style="font-size:1rem;margin:0 0 .5rem"><?= t('update.current_version') ?></h2>
    <div style="font-size:.9rem">
        <strong><?= htmlspecialchars($settings->get('app_title', t('app.title'))) ?></strong>
        &mdash; v<?= APP_VERSION ?>
    </div>
    <div style="font-size:.82rem;color:var(--color-text-muted);margin-top:.2rem">
        PHP <?= PHP_VERSION ?>
    </div>
</div>

<!-- Upload form -->
<div class="card" style="margin-bottom:1.2rem">
    <h2 style="font-size:1rem;margin:0 0 .5rem"><?= t('update.upload_title') ?></h2>
    <p style="font-size:.85rem;color:var(--color-text-muted);margin:0 0 .75rem">
        <?= t('update.upload_hint') ?>
    </p>
    <p class="alert alert-warning" style="font-size:.82rem;margin:0 0 .75rem">
        ⚠️ <?= t('update.danger_hint') ?>
    </p>

    <form method="post" enctype="multipart/form-data">
        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken) ?>">

        <div class="form-group">
            <label for="update_zip"><?= t('update.zip_label') ?></label>
            <input type="file" id="update_zip" name="update_zip" accept=".zip" required>
        </div>

        <div class="form-actions">
            <button type="submit" class="btn btn-primary"
                    onclick="return confirm('<?= htmlspecialchars(t('update.danger_hint')) ?>\n\n<?= htmlspecialchars(t('update.confirm')) ?>')">
                🔄 <?= t('update.apply') ?>
            </button>
        </div>
    </form>
</div>

<!-- Recent backups (previous versions) -->
<div class="card" style="margin-bottom:1.2rem">
    <h2 style="font-size:1rem;margin:0 0 .5rem"><?= t('update.backups_title') ?></h2>
    <p style="font-size:.85rem;color:var(--color-text-muted);margin:0 0 .75rem">
        <?= t('update.backups_hint') ?>
    </p>
    <?php if (empty($backups)): ?>
        <p style="font-size:.85rem;color:var(--color-text-muted);margin:0"><?= t('update.backups_empty') ?></p>
    <?php else: ?>
    <table style="width:100%;border-collapse:collapse;font-size:.85rem">
        <tbody>
        <?php foreach ($backups as $idx => $b): ?>
            <tr style="border-top:1px solid var(--color-border)">
                <td style="padding:.5rem .4rem">
                    <code><?= htmlspecialchars($b['name']) ?></code>
                    <?php if ($idx === 0): ?>
                        <span class="badge" style="font-size:.72rem;margin-left:.35rem"><?= t('update.backup_latest') ?></span>
                    <?php endif; ?>
                </td>
                <td style="padding:.5rem .4rem;color:var(--color-text-muted);white-space:nowrap">
                    <?= date('Y-m-d H:i:s', $b['time']) ?>
                </td>
                <td style="padding:.5rem .4rem;color:var(--color-text-muted);white-space:nowrap;text-align:right">
                    <?= number_format($b['size'] / 1024, 0, '.', ' ') ?> KB
                </td>
                <td style="padding:.5rem .4rem;text-align:right;white-space:nowrap">
                    <form method="post" style="display:inline;margin:0">
                        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken) ?>">
                        <input type="hidden" name="action" value="restore_backup">
                        <input type="hidden" name="backup" value="<?= htmlspecialchars($b['name']) ?>">
                        <button type="submit" class="btn btn-sm btn-secondary"
                                onclick="return confirm('<?= htmlspecialchars(t('update.restore_confirm')) ?>')">
                            ⤺ <?= t('update.restore') ?>
                        </button>
                    </form>
                    <form method="post" style="display:inline;margin:0">
                        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken) ?>">
                        <input type="hidden" name="action" value="delete_backup">
                        <input type="hidden" name="backup" value="<?= htmlspecialchars($b['name']) ?>">
                        <button type="submit" class="btn btn-sm btn-danger"
                                onclick="return confirm('<?= htmlspecialchars(t('update.delete_backup_confirm')) ?>')">
                            🗑 <?= t('update.delete_backup') ?>
                        </button>
                    </form>
                </td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
    <?php endif; ?>
</div>

<!-- Protected files info -->
<div class="card">
    <h2 style="font-size:1rem;margin:0 0 .5rem"><?= t('update.protected_title') ?></h2>
    <p style="font-size:.85rem;color:var(--color-text-muted);margin:0 0 .5rem">
        <?= t('update.protected_hint') ?>
    </p>
    <ul style="font-size:.84rem;margin:0;padding-left:1.4rem;line-height:1.8">
        <li><code>securite/database.sqlite</code> &mdash; <?= t('update.protected_db') ?></li>
        <li><code>securite/master.key</code> &mdash; <?= t('update.protected_key') ?></li>
        <li><code>securite/wallets/*</code> &mdash; <?= t('update.protected_wallets') ?></li>
        <li><code>securite/</code>, <code>log/</code> &mdash; <?= t('update.protected_dirs') ?></li>
        <li><code>.git</code> &mdash; <?= $keepGit ? t('update.git_kept') : t('update.git_removed') ?></li>
    </ul>
</div>

</div>
</div>

<?php include WEBROOT_PATH . '/includes/footer.php'; ?>
