<div class="content-wrapper">
<section class="content pt-3">
<div class="container-fluid pb-5">

    <div class="d-flex justify-content-between align-items-start gap-3 mb-4">
        <div>
            <h3 class="mb-1 fw-bold"><i class="bi bi-trash3-fill text-danger me-2"></i><?= __('system.cleanup_title', 'System Cleanup') ?></h3>
            <div class="text-muted small"><?= __('system.cleanup_desc', 'Clean temporary tables and heavy logs without touching business data.') ?></div>
        </div>
        <a href="/admin/system/errors" class="btn btn-outline-danger"><i class="bi bi-bug-fill me-1"></i><?= __('system.error_logs', 'Error Logs') ?></a>
    </div>

    <?php if (isset($_GET['done'])): ?>
        <div class="alert alert-success">
            <?= __('system.cleanup_done', 'Cleanup completed successfully.') ?>
            <?php if (isset($_GET['rows'])): ?>
                <span class="fw-semibold ms-2">(<?= (int)$_GET['rows'] ?>)</span>
            <?php endif; ?>
        </div>
    <?php endif; ?>

    <?php if (isset($_GET['error'])): ?>
        <div class="alert alert-danger"><?= __('system.cleanup_error', 'Invalid cleanup action.') ?></div>
    <?php endif; ?>

    <div class="row g-4">
        <?php
        $cards = [
            [
                'icon' => 'bi-bell-fill', 'title' => __('system.notifications', 'Notifications'), 'count' => (int)($stats['admin_notifications'] ?? 0),
                'forms' => [
                    ['action' => 'purge_notifications_old', 'days' => 30, 'btn' => 'btn-outline-warning', 'label' => __('system.delete_old_notifications','Delete old notifications')],
                    ['action' => 'purge_notifications_all', 'danger' => true, 'btn' => 'btn-danger', 'label' => __('system.delete_all_notifications','Delete all notifications')],
                ],
                'hint' => __('system.notifications_hint', 'Safe to clean old notifications regularly.'),
            ],
            [
                'icon' => 'bi-clock-history', 'title' => __('system.admin_activity', 'Admin activity log'), 'count' => (int)($stats['admin_activity_log'] ?? 0),
                'forms' => [
                    ['action' => 'purge_admin_activity_old', 'days' => 30, 'btn' => 'btn-outline-secondary', 'label' => __('system.delete_old_activity','Delete old admin activity')],
                    ['action' => 'purge_admin_activity_all', 'danger' => true, 'btn' => 'btn-danger', 'label' => __('system.delete_all_activity','Delete all admin activity')],
                ],
                'hint' => __('system.activity_hint', 'Login/logout history only.'),
            ],
            [
                'icon' => 'bi-arrow-repeat', 'title' => __('system.password_resets', 'Password reset requests'), 'count' => (int)($stats['admin_password_resets'] ?? 0),
                'forms' => [
                    ['action' => 'purge_password_resets_expired', 'btn' => 'btn-outline-secondary', 'label' => __('system.delete_expired_resets','Delete expired reset requests')],
                    ['action' => 'purge_password_resets_old', 'days' => 7, 'btn' => 'btn-outline-warning', 'label' => __('system.delete_old_resets','Delete old reset requests')],
                ],
                'hint' => __('system.resets_hint', 'Expired reset tokens are safe to remove.'),
            ],
            [
                'icon' => 'bi-eye-fill', 'title' => __('system.page_views', 'Page Views'), 'count' => (int)($stats['page_views'] ?? 0),
                'forms' => [
                    ['action' => 'purge_page_views_old', 'days' => 30, 'btn' => 'btn-outline-secondary', 'label' => __('system.delete_old_page_views','Delete old page views')],
                    ['action' => 'purge_page_views_all', 'danger' => true, 'btn' => 'btn-danger', 'label' => __('system.delete_all_page_views','Delete all page views')],
                ],
                'hint' => __('system.page_views_hint', 'Analytics cache only; does not affect orders or products.'),
            ],
            [
                'icon' => 'bi-shield-lock-fill', 'title' => __('system.security_logs', 'Security Logs'), 'count' => (int)($stats['security_logs'] ?? 0),
                'forms' => [
                    ['action' => 'purge_security_404_7', 'btn' => 'btn-outline-danger', 'label' => __('system.clean_404_7', 'Clean 404 older than 7 days')],
                    ['action' => 'purge_security_warning_30', 'btn' => 'btn-outline-warning', 'label' => __('system.clean_warning_30', 'Clean warnings older than 30 days')],
                    ['action' => 'purge_security_error_30', 'btn' => 'btn-outline-primary', 'label' => __('system.clean_error_30', 'Clean errors older than 30 days')],
                    ['action' => 'purge_security_logs_all', 'danger' => true, 'btn' => 'btn-danger', 'label' => __('system.delete_all_security_logs', 'Delete all security logs')],
                ],
                'hint' => __('system.security_hint', 'Recommended: 404 keep 7 days, errors keep 30 days.'),
            ],
            [
                'icon' => 'bi-speedometer2', 'title' => __('system.rate_limits', 'Rate Limits'), 'count' => (int)($stats['security_rate_limits'] ?? 0),
                'forms' => [
                    ['action' => 'purge_rate_limits_old', 'days' => 7, 'btn' => 'btn-outline-secondary', 'label' => __('system.delete_old_rate_limits', 'Delete old rate limits')],
                    ['action' => 'purge_rate_limits_all', 'danger' => true, 'btn' => 'btn-danger', 'label' => __('system.delete_all_rate_limits', 'Delete all rate limits')],
                ],
                'hint' => __('system.rate_limits_hint', 'Temporary throttling data only.'),
            ],
            [
                'icon' => 'bi-slash-circle-fill', 'title' => __('system.fraud_attempts', 'Rejected Fraud Attempts'), 'count' => (int)($stats['fraud_attempts'] ?? 0),
                'forms' => [
                    ['action' => 'purge_fraud_attempts_old', 'days' => 90, 'btn' => 'btn-outline-danger', 'label' => __('system.delete_old', 'Delete old records')],
                    ['action' => 'purge_fraud_attempts_all', 'danger' => true, 'btn' => 'btn-danger', 'label' => __('system.delete_all_fraud_attempts', 'Delete all fraud attempts')],
                ],
                'hint' => __('system.fraud_attempts_hint', 'Recommended: keep last 90 days.'),
            ],
            [
                'icon' => 'bi-cloud-arrow-up-fill', 'title' => __('system.updater_jobs', 'Updater jobs'), 'count' => (int)($stats['safe_updater_jobs'] ?? 0),
                'forms' => [
                    ['action' => 'purge_updater_jobs_old', 'days' => 30, 'btn' => 'btn-outline-secondary', 'label' => __('system.delete_old_updater_jobs', 'Delete old finished updater jobs')],
                    ['action' => 'purge_updater_jobs_finished', 'danger' => true, 'btn' => 'btn-danger', 'label' => __('system.delete_finished_updater_jobs', 'Delete all finished updater jobs')],
                ],
                'hint' => __('system.updater_jobs_hint', 'Running jobs are kept. Only finished/failed jobs are deleted.'),
            ],
            [
                'icon' => 'bi-whatsapp', 'title' => __('system.whatsapp_messages', 'WhatsApp messages log'), 'count' => (int)($stats['whatsapp_messages'] ?? 0),
                'forms' => [
                    ['action' => 'purge_whatsapp_messages_old', 'days' => 60, 'btn' => 'btn-outline-success', 'label' => __('system.delete_old_whatsapp', 'Delete old WhatsApp logs')],
                    ['action' => 'purge_whatsapp_messages_all', 'danger' => true, 'btn' => 'btn-danger', 'label' => __('system.delete_all_whatsapp', 'Delete all WhatsApp logs')],
                ],
                'hint' => __('system.whatsapp_hint', 'Useful when message history becomes too large.'),
            ],
            [
                'icon' => 'bi-magic', 'title' => __('system.ai_landing_drafts', 'AI landing drafts'), 'count' => (int)($stats['ai_landing_pages'] ?? 0),
                'forms' => [
                    ['action' => 'purge_ai_landing_drafts_old', 'days' => 30, 'btn' => 'btn-outline-dark', 'label' => __('system.delete_old_ai_drafts', 'Delete old draft landing pages')],
                ],
                'hint' => __('system.ai_drafts_hint', 'Only draft landing pages are removed. Published pages stay safe.'),
            ],
        ];
        ?>

        <?php foreach ($cards as $card): ?>
            <div class="col-12 col-md-6 col-xl-4">
                <div class="card border-0 shadow-sm h-100">
                    <div class="card-header fw-bold d-flex align-items-center justify-content-between">
                        <span><i class="bi <?= $card['icon'] ?> me-2"></i><?= $card['title'] ?></span>
                        <span class="badge bg-light text-dark"><?= $card['count'] ?></span>
                    </div>
                    <div class="card-body d-grid gap-2">
                        <?php foreach ($card['forms'] as $form): ?>
                            <form method="POST" action="/admin/system/cleanup/run" class="d-grid gap-2">
                                <?= \Core\System\Csrf::field(); ?>
                                <input type="hidden" name="action" value="<?= htmlspecialchars($form['action'], ENT_QUOTES) ?>">
                                <?php if (isset($form['days'])): ?>
                                    <div class="input-group">
                                        <span class="input-group-text"><?= __('system.days', 'Days') ?></span>
                                        <input type="number" class="form-control" name="days" value="<?= (int)$form['days'] ?>" min="1">
                                    </div>
                                <?php endif; ?>
                                <button class="btn <?= $form['btn'] ?>" type="submit" <?= !empty($form['danger']) ? "onclick=\"return confirm('" . __('system.confirm_delete_all','Are you sure?') . "')\"" : '' ?>>
                                    <i class="bi bi-trash3 me-1"></i><?= $form['label'] ?>
                                </button>
                            </form>
                        <?php endforeach; ?>
                        <small class="text-muted mt-1"><?= $card['hint'] ?></small>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>
    </div>

</div>
</section>
</div>
