<?php
/**
 * Database Cleaner Admin Page Template
 */

// Prevent direct access
if (!defined('ABSPATH')) exit;

$settings = get_option('switchboard_database-cleaner_settings', []);
$auto_clean = isset($settings['auto_clean']) ? $settings['auto_clean'] : 'disabled';
$auto_clean_items = isset($settings['auto_clean_items']) ? $settings['auto_clean_items'] : [];
$batch_size = isset($settings['batch_size']) ? $settings['batch_size'] : 100;
?>

<div class="wrap sb-module-wrap switchboard-database-cleaner">
    <!-- Header -->
    <div class="sb-module-header">
        <div class="sb-module-header-left">
            <svg class="sb-module-title-icon" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path>
            </svg>
            <div>
                <h1 class="sb-module-title">
                    <?php esc_html_e('Database Cleaner', 'wpswitchboard'); ?>
                    <span class="switchboard-badge">SWITCHBOARD PRO</span>
                </h1>
                <p class="sb-module-subtitle">
                    <?php esc_html_e('Clean and optimize your WordPress database for better performance', 'wpswitchboard'); ?>
                </p>
            </div>
        </div>
        <div class="sb-module-header-right">
            <a href="https://docs.wpswitchboard.com/modules/optimization/database-cleaner" target="_blank" class="sb-module-help-link">
                <span class="sb-module-help-link-icon dashicons dashicons-editor-help"></span>
                Help
            </a>
            <button type="button" class="sb-module-button sb-module-button-primary dbcleaner-action-btn" id="dbcleaner-analyze-btn">
                <span class="dashicons dashicons-update"></span>
                <?php esc_html_e('Refresh', 'wpswitchboard'); ?>
            </button>
        </div>
    </div>

    <!-- Main Content -->
    <div class="sb-module-container">
        <!-- Stats Cards -->
        <div class="sb-module-stats">
            <div class="sb-module-stat-card">
                <div class="sb-module-stat-label"><?php esc_html_e('Database Size', 'wpswitchboard'); ?></div>
                <div class="sb-module-stat-content">
                    <div class="sb-module-stat-value" id="stat-db-size">--</div>
                </div>
            </div>
            <div class="sb-module-stat-card">
                <div class="sb-module-stat-label"><?php esc_html_e('Cleanable Items', 'wpswitchboard'); ?></div>
                <div class="sb-module-stat-content">
                    <div class="sb-module-stat-value" id="stat-cleanable">--</div>
                </div>
            </div>
            <div class="sb-module-stat-card">
                <div class="sb-module-stat-label"><?php esc_html_e('Potential Savings', 'wpswitchboard'); ?></div>
                <div class="sb-module-stat-content">
                    <div class="sb-module-stat-value" id="stat-savings">--</div>
                </div>
            </div>
            <div class="sb-module-stat-card">
                <div class="sb-module-stat-label"><?php esc_html_e('Last Cleaned', 'wpswitchboard'); ?></div>
                <div class="sb-module-stat-content">
                    <div class="sb-module-stat-value" id="stat-last-clean">--</div>
                </div>
            </div>
        </div>

    <!-- Tab Navigation -->
    <div class="dbcleaner-tabs">
        <button type="button" class="dbcleaner-tab active" data-tab="cleanup">
            <span class="dashicons dashicons-trash"></span>
            <?php esc_html_e('Cleanup', 'wpswitchboard'); ?>
        </button>
        <button type="button" class="dbcleaner-tab" data-tab="tables">
            <span class="dashicons dashicons-editor-table"></span>
            <?php esc_html_e('Tables', 'wpswitchboard'); ?>
        </button>
        <button type="button" class="dbcleaner-tab" data-tab="options">
            <span class="dashicons dashicons-admin-settings"></span>
            <?php esc_html_e('Options', 'wpswitchboard'); ?>
        </button>
        <button type="button" class="dbcleaner-tab" data-tab="metadata">
            <span class="dashicons dashicons-tag"></span>
            <?php esc_html_e('Metadata', 'wpswitchboard'); ?>
        </button>
        <button type="button" class="dbcleaner-tab" data-tab="logs">
            <span class="dashicons dashicons-list-view"></span>
            <?php esc_html_e('Logs', 'wpswitchboard'); ?>
        </button>
        <button type="button" class="dbcleaner-tab" data-tab="settings">
            <span class="dashicons dashicons-admin-generic"></span>
            <?php esc_html_e('Settings', 'wpswitchboard'); ?>
        </button>
    </div>

    <!-- Tab Content: Cleanup -->
    <div class="dbcleaner-tab-content active" id="tab-cleanup">
        <!-- Posts Category -->
        <div class="dbcleaner-category-card">
            <div class="category-header" data-category="posts">
                <div class="category-title">
                    <span class="category-icon"></span>
                    <span class="category-name"><?php esc_html_e('Posts', 'wpswitchboard'); ?></span>
                    <span class="category-count" data-category-count="posts">0 <?php esc_html_e('items', 'wpswitchboard'); ?></span>
                </div>
                <span class="category-toggle dashicons dashicons-arrow-down-alt2"></span>
            </div>
            <div class="category-content">
                <div class="cleanup-item" data-item="revisions" data-risk="safe">
                    <label class="item-checkbox">
                        <input type="checkbox" checked>
                        <span class="checkmark"></span>
                    </label>
                    <div class="item-info">
                        <div class="item-label"><?php esc_html_e('Revisions', 'wpswitchboard'); ?></div>
                        <div class="item-description"><?php esc_html_e('Post revisions stored for undo/history', 'wpswitchboard'); ?></div>
                    </div>
                    <div class="item-stats">
                        <span class="item-count" data-count="revisions">--</span>
                        <span class="item-size" data-size="revisions">--</span>
                    </div>
                    <button type="button" class="button button-small item-clean-btn" data-item="revisions">
                        <?php esc_html_e('Clean', 'wpswitchboard'); ?>
                    </button>
                </div>

                <div class="cleanup-item" data-item="auto_drafts" data-risk="safe">
                    <label class="item-checkbox">
                        <input type="checkbox" checked>
                        <span class="checkmark"></span>
                    </label>
                    <div class="item-info">
                        <div class="item-label"><?php esc_html_e('Auto-Drafts', 'wpswitchboard'); ?></div>
                        <div class="item-description"><?php esc_html_e('Automatically saved drafts', 'wpswitchboard'); ?></div>
                    </div>
                    <div class="item-stats">
                        <span class="item-count" data-count="auto_drafts">--</span>
                        <span class="item-size" data-size="auto_drafts">--</span>
                    </div>
                    <button type="button" class="button button-small item-clean-btn" data-item="auto_drafts">
                        <?php esc_html_e('Clean', 'wpswitchboard'); ?>
                    </button>
                </div>

                <div class="cleanup-item" data-item="trashed_posts" data-risk="safe">
                    <label class="item-checkbox">
                        <input type="checkbox" checked>
                        <span class="checkmark"></span>
                    </label>
                    <div class="item-info">
                        <div class="item-label"><?php esc_html_e('Trashed Posts', 'wpswitchboard'); ?></div>
                        <div class="item-description"><?php esc_html_e('Posts and pages in trash', 'wpswitchboard'); ?></div>
                    </div>
                    <div class="item-stats">
                        <span class="item-count" data-count="trashed_posts">--</span>
                        <span class="item-size" data-size="trashed_posts">--</span>
                    </div>
                    <button type="button" class="button button-small item-clean-btn" data-item="trashed_posts">
                        <?php esc_html_e('Clean', 'wpswitchboard'); ?>
                    </button>
                </div>
            </div>
        </div>

        <!-- Comments Category -->
        <div class="dbcleaner-category-card">
            <div class="category-header" data-category="comments">
                <div class="category-title">
                    <span class="category-icon"></span>
                    <span class="category-name"><?php esc_html_e('Comments', 'wpswitchboard'); ?></span>
                    <span class="category-count" data-category-count="comments">0 <?php esc_html_e('items', 'wpswitchboard'); ?></span>
                </div>
                <span class="category-toggle dashicons dashicons-arrow-down-alt2"></span>
            </div>
            <div class="category-content">
                <div class="cleanup-item" data-item="spam_comments" data-risk="safe">
                    <label class="item-checkbox">
                        <input type="checkbox" checked>
                        <span class="checkmark"></span>
                    </label>
                    <div class="item-info">
                        <div class="item-label"><?php esc_html_e('Spam Comments', 'wpswitchboard'); ?></div>
                        <div class="item-description"><?php esc_html_e('Comments marked as spam', 'wpswitchboard'); ?></div>
                    </div>
                    <div class="item-stats">
                        <span class="item-count" data-count="spam_comments">--</span>
                        <span class="item-size" data-size="spam_comments">--</span>
                    </div>
                    <button type="button" class="button button-small item-clean-btn" data-item="spam_comments">
                        <?php esc_html_e('Clean', 'wpswitchboard'); ?>
                    </button>
                </div>

                <div class="cleanup-item" data-item="trashed_comments" data-risk="safe">
                    <label class="item-checkbox">
                        <input type="checkbox" checked>
                        <span class="checkmark"></span>
                    </label>
                    <div class="item-info">
                        <div class="item-label"><?php esc_html_e('Trashed Comments', 'wpswitchboard'); ?></div>
                        <div class="item-description"><?php esc_html_e('Comments in trash', 'wpswitchboard'); ?></div>
                    </div>
                    <div class="item-stats">
                        <span class="item-count" data-count="trashed_comments">--</span>
                        <span class="item-size" data-size="trashed_comments">--</span>
                    </div>
                    <button type="button" class="button button-small item-clean-btn" data-item="trashed_comments">
                        <?php esc_html_e('Clean', 'wpswitchboard'); ?>
                    </button>
                </div>

                <div class="cleanup-item" data-item="unapproved_comments" data-risk="caution">
                    <label class="item-checkbox">
                        <input type="checkbox">
                        <span class="checkmark"></span>
                    </label>
                    <div class="item-info">
                        <div class="item-label">
                            <?php esc_html_e('Unapproved Comments', 'wpswitchboard'); ?>
                            <span class="risk-badge caution"><?php esc_html_e('Caution', 'wpswitchboard'); ?></span>
                        </div>
                        <div class="item-description"><?php esc_html_e('Comments pending moderation', 'wpswitchboard'); ?></div>
                    </div>
                    <div class="item-stats">
                        <span class="item-count" data-count="unapproved_comments">--</span>
                        <span class="item-size" data-size="unapproved_comments">--</span>
                    </div>
                    <button type="button" class="button button-small item-clean-btn" data-item="unapproved_comments">
                        <?php esc_html_e('Clean', 'wpswitchboard'); ?>
                    </button>
                </div>

                <div class="cleanup-item" data-item="pingbacks" data-risk="caution">
                    <label class="item-checkbox">
                        <input type="checkbox">
                        <span class="checkmark"></span>
                    </label>
                    <div class="item-info">
                        <div class="item-label">
                            <?php esc_html_e('Pingbacks & Trackbacks', 'wpswitchboard'); ?>
                            <span class="risk-badge caution"><?php esc_html_e('Caution', 'wpswitchboard'); ?></span>
                        </div>
                        <div class="item-description"><?php esc_html_e('Auto-generated pingback/trackback comments', 'wpswitchboard'); ?></div>
                    </div>
                    <div class="item-stats">
                        <span class="item-count" data-count="pingbacks">--</span>
                        <span class="item-size" data-size="pingbacks">--</span>
                    </div>
                    <button type="button" class="button button-small item-clean-btn" data-item="pingbacks">
                        <?php esc_html_e('Clean', 'wpswitchboard'); ?>
                    </button>
                </div>
            </div>
        </div>

        <!-- Metadata Category -->
        <div class="dbcleaner-category-card">
            <div class="category-header" data-category="metadata">
                <div class="category-title">
                    <span class="category-icon"></span>
                    <span class="category-name"><?php esc_html_e('Metadata', 'wpswitchboard'); ?></span>
                    <span class="category-count" data-category-count="metadata">0 <?php esc_html_e('items', 'wpswitchboard'); ?></span>
                </div>
                <span class="category-toggle dashicons dashicons-arrow-down-alt2"></span>
            </div>
            <div class="category-content">
                <div class="cleanup-item" data-item="orphaned_postmeta" data-risk="safe">
                    <label class="item-checkbox">
                        <input type="checkbox" checked>
                        <span class="checkmark"></span>
                    </label>
                    <div class="item-info">
                        <div class="item-label"><?php esc_html_e('Orphaned Post Meta', 'wpswitchboard'); ?></div>
                        <div class="item-description"><?php esc_html_e('Post metadata with no parent post', 'wpswitchboard'); ?></div>
                    </div>
                    <div class="item-stats">
                        <span class="item-count" data-count="orphaned_postmeta">--</span>
                        <span class="item-size" data-size="orphaned_postmeta">--</span>
                    </div>
                    <button type="button" class="button button-small item-clean-btn" data-item="orphaned_postmeta">
                        <?php esc_html_e('Clean', 'wpswitchboard'); ?>
                    </button>
                </div>

                <div class="cleanup-item" data-item="duplicated_postmeta" data-risk="caution">
                    <label class="item-checkbox">
                        <input type="checkbox">
                        <span class="checkmark"></span>
                    </label>
                    <div class="item-info">
                        <div class="item-label">
                            <?php esc_html_e('Duplicated Post Meta', 'wpswitchboard'); ?>
                            <span class="risk-badge caution"><?php esc_html_e('Caution', 'wpswitchboard'); ?></span>
                        </div>
                        <div class="item-description"><?php esc_html_e('Duplicate metadata entries', 'wpswitchboard'); ?></div>
                    </div>
                    <div class="item-stats">
                        <span class="item-count" data-count="duplicated_postmeta">--</span>
                        <span class="item-size" data-size="duplicated_postmeta">--</span>
                    </div>
                    <button type="button" class="button button-small item-clean-btn" data-item="duplicated_postmeta">
                        <?php esc_html_e('Clean', 'wpswitchboard'); ?>
                    </button>
                </div>

                <div class="cleanup-item" data-item="orphaned_commentmeta" data-risk="safe">
                    <label class="item-checkbox">
                        <input type="checkbox" checked>
                        <span class="checkmark"></span>
                    </label>
                    <div class="item-info">
                        <div class="item-label"><?php esc_html_e('Orphaned Comment Meta', 'wpswitchboard'); ?></div>
                        <div class="item-description"><?php esc_html_e('Comment metadata with no parent comment', 'wpswitchboard'); ?></div>
                    </div>
                    <div class="item-stats">
                        <span class="item-count" data-count="orphaned_commentmeta">--</span>
                        <span class="item-size" data-size="orphaned_commentmeta">--</span>
                    </div>
                    <button type="button" class="button button-small item-clean-btn" data-item="orphaned_commentmeta">
                        <?php esc_html_e('Clean', 'wpswitchboard'); ?>
                    </button>
                </div>

                <div class="cleanup-item" data-item="orphaned_usermeta" data-risk="safe">
                    <label class="item-checkbox">
                        <input type="checkbox" checked>
                        <span class="checkmark"></span>
                    </label>
                    <div class="item-info">
                        <div class="item-label"><?php esc_html_e('Orphaned User Meta', 'wpswitchboard'); ?></div>
                        <div class="item-description"><?php esc_html_e('User metadata with no parent user', 'wpswitchboard'); ?></div>
                    </div>
                    <div class="item-stats">
                        <span class="item-count" data-count="orphaned_usermeta">--</span>
                        <span class="item-size" data-size="orphaned_usermeta">--</span>
                    </div>
                    <button type="button" class="button button-small item-clean-btn" data-item="orphaned_usermeta">
                        <?php esc_html_e('Clean', 'wpswitchboard'); ?>
                    </button>
                </div>

                <div class="cleanup-item" data-item="orphaned_termmeta" data-risk="safe">
                    <label class="item-checkbox">
                        <input type="checkbox" checked>
                        <span class="checkmark"></span>
                    </label>
                    <div class="item-info">
                        <div class="item-label"><?php esc_html_e('Orphaned Term Meta', 'wpswitchboard'); ?></div>
                        <div class="item-description"><?php esc_html_e('Term metadata with no parent term', 'wpswitchboard'); ?></div>
                    </div>
                    <div class="item-stats">
                        <span class="item-count" data-count="orphaned_termmeta">--</span>
                        <span class="item-size" data-size="orphaned_termmeta">--</span>
                    </div>
                    <button type="button" class="button button-small item-clean-btn" data-item="orphaned_termmeta">
                        <?php esc_html_e('Clean', 'wpswitchboard'); ?>
                    </button>
                </div>

                <div class="cleanup-item" data-item="oembed_cache" data-risk="safe">
                    <label class="item-checkbox">
                        <input type="checkbox" checked>
                        <span class="checkmark"></span>
                    </label>
                    <div class="item-info">
                        <div class="item-label"><?php esc_html_e('oEmbed Caches', 'wpswitchboard'); ?></div>
                        <div class="item-description"><?php esc_html_e('Cached embed data from external services', 'wpswitchboard'); ?></div>
                    </div>
                    <div class="item-stats">
                        <span class="item-count" data-count="oembed_cache">--</span>
                        <span class="item-size" data-size="oembed_cache">--</span>
                    </div>
                    <button type="button" class="button button-small item-clean-btn" data-item="oembed_cache">
                        <?php esc_html_e('Clean', 'wpswitchboard'); ?>
                    </button>
                </div>
            </div>
        </div>

        <!-- Transients Category -->
        <div class="dbcleaner-category-card">
            <div class="category-header" data-category="transients">
                <div class="category-title">
                    <span class="category-icon"></span>
                    <span class="category-name"><?php esc_html_e('Transients', 'wpswitchboard'); ?></span>
                    <span class="category-count" data-category-count="transients">0 <?php esc_html_e('items', 'wpswitchboard'); ?></span>
                </div>
                <span class="category-toggle dashicons dashicons-arrow-down-alt2"></span>
            </div>
            <div class="category-content">
                <div class="cleanup-item" data-item="expired_transients" data-risk="safe">
                    <label class="item-checkbox">
                        <input type="checkbox" checked>
                        <span class="checkmark"></span>
                    </label>
                    <div class="item-info">
                        <div class="item-label"><?php esc_html_e('Expired Transients', 'wpswitchboard'); ?></div>
                        <div class="item-description"><?php esc_html_e('Temporary data that has expired', 'wpswitchboard'); ?></div>
                    </div>
                    <div class="item-stats">
                        <span class="item-count" data-count="expired_transients">--</span>
                        <span class="item-size" data-size="expired_transients">--</span>
                    </div>
                    <button type="button" class="button button-small item-clean-btn" data-item="expired_transients">
                        <?php esc_html_e('Clean', 'wpswitchboard'); ?>
                    </button>
                </div>

                <div class="cleanup-item" data-item="all_transients" data-risk="risky">
                    <label class="item-checkbox">
                        <input type="checkbox">
                        <span class="checkmark"></span>
                    </label>
                    <div class="item-info">
                        <div class="item-label">
                            <?php esc_html_e('All Transients', 'wpswitchboard'); ?>
                            <span class="risk-badge risky"><?php esc_html_e('Risky', 'wpswitchboard'); ?></span>
                        </div>
                        <div class="item-description"><?php esc_html_e('All temporary cached data (rebuilds on demand)', 'wpswitchboard'); ?></div>
                    </div>
                    <div class="item-stats">
                        <span class="item-count" data-count="all_transients">--</span>
                        <span class="item-size" data-size="all_transients">--</span>
                    </div>
                    <button type="button" class="button button-small item-clean-btn" data-item="all_transients">
                        <?php esc_html_e('Clean', 'wpswitchboard'); ?>
                    </button>
                </div>
            </div>
        </div>

        <!-- Terms Category -->
        <div class="dbcleaner-category-card">
            <div class="category-header" data-category="terms">
                <div class="category-title">
                    <span class="category-icon"></span>
                    <span class="category-name"><?php esc_html_e('Terms', 'wpswitchboard'); ?></span>
                    <span class="category-count" data-category-count="terms">0 <?php esc_html_e('items', 'wpswitchboard'); ?></span>
                </div>
                <span class="category-toggle dashicons dashicons-arrow-down-alt2"></span>
            </div>
            <div class="category-content">
                <div class="cleanup-item" data-item="unused_terms" data-risk="caution">
                    <label class="item-checkbox">
                        <input type="checkbox">
                        <span class="checkmark"></span>
                    </label>
                    <div class="item-info">
                        <div class="item-label">
                            <?php esc_html_e('Unused Terms', 'wpswitchboard'); ?>
                            <span class="risk-badge caution"><?php esc_html_e('Caution', 'wpswitchboard'); ?></span>
                        </div>
                        <div class="item-description"><?php esc_html_e('Categories/tags with no posts', 'wpswitchboard'); ?></div>
                    </div>
                    <div class="item-stats">
                        <span class="item-count" data-count="unused_terms">--</span>
                        <span class="item-size" data-size="unused_terms">N/A</span>
                    </div>
                    <button type="button" class="button button-small item-clean-btn" data-item="unused_terms">
                        <?php esc_html_e('Clean', 'wpswitchboard'); ?>
                    </button>
                </div>

                <div class="cleanup-item" data-item="orphaned_term_relationships" data-risk="safe">
                    <label class="item-checkbox">
                        <input type="checkbox" checked>
                        <span class="checkmark"></span>
                    </label>
                    <div class="item-info">
                        <div class="item-label"><?php esc_html_e('Orphaned Term Relationships', 'wpswitchboard'); ?></div>
                        <div class="item-description"><?php esc_html_e('Term relationships with missing terms', 'wpswitchboard'); ?></div>
                    </div>
                    <div class="item-stats">
                        <span class="item-count" data-count="orphaned_term_relationships">--</span>
                        <span class="item-size" data-size="orphaned_term_relationships">N/A</span>
                    </div>
                    <button type="button" class="button button-small item-clean-btn" data-item="orphaned_term_relationships">
                        <?php esc_html_e('Clean', 'wpswitchboard'); ?>
                    </button>
                </div>
            </div>
        </div>

        <!-- Bulk Actions -->
        <div class="dbcleaner-bulk-actions">
            <button type="button" class="button button-primary" id="clean-selected-btn">
                <span class="dashicons dashicons-trash"></span>
                <?php esc_html_e('Clean Selected', 'wpswitchboard'); ?>
            </button>
            <button type="button" class="button" id="clean-all-safe-btn">
                <span class="dashicons dashicons-yes-alt"></span>
                <?php esc_html_e('Clean All Safe Items', 'wpswitchboard'); ?>
            </button>
        </div>
    </div>

    <!-- Tab Content: Tables -->
    <div class="dbcleaner-tab-content" id="tab-tables">
        <div class="sb-module-card">
            <div class="sb-module-card-header">
                <h2 class="sb-module-card-title"><?php esc_html_e('Database Tables', 'wpswitchboard'); ?></h2>
                <p class="sb-module-card-desc"><?php esc_html_e('View and optimize database tables. Large tables may benefit from optimization.', 'wpswitchboard'); ?></p>
            </div>
            <div class="sb-module-card-body">
                <div class="sb-module-table-wrapper">
                    <table class="sb-module-table" id="tables-list">
                        <thead>
                            <tr>
                                <th><?php esc_html_e('Table Name', 'wpswitchboard'); ?></th>
                                <th><?php esc_html_e('Rows', 'wpswitchboard'); ?></th>
                                <th><?php esc_html_e('Size', 'wpswitchboard'); ?></th>
                                <th><?php esc_html_e('% of DB', 'wpswitchboard'); ?></th>
                                <th><?php esc_html_e('Engine', 'wpswitchboard'); ?></th>
                                <th><?php esc_html_e('Actions', 'wpswitchboard'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr class="loading-row">
                                <td colspan="6" class="loading-cell">
                                    <span class="spinner is-active"></span>
                                    <?php esc_html_e('Loading tables...', 'wpswitchboard'); ?>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <!-- Tab Content: Options -->
    <div class="dbcleaner-tab-content" id="tab-options">
        <div class="sb-module-card">
            <div class="sb-module-card-header">
                <h2 class="sb-module-card-title"><?php esc_html_e('WordPress Options', 'wpswitchboard'); ?></h2>
                <p class="sb-module-card-desc"><?php esc_html_e('Browse and manage options in the wp_options table. Core WordPress options are protected.', 'wpswitchboard'); ?></p>
            </div>
            <div class="sb-module-card-body">
                <div class="dbcleaner-filters">
                    <input type="text" id="options-search" placeholder="<?php esc_attr_e('Search options...', 'wpswitchboard'); ?>" class="regular-text">
                    <select id="options-autoload">
                        <option value="all"><?php esc_html_e('All Options', 'wpswitchboard'); ?></option>
                        <option value="yes"><?php esc_html_e('Autoload: Yes', 'wpswitchboard'); ?></option>
                        <option value="no"><?php esc_html_e('Autoload: No', 'wpswitchboard'); ?></option>
                    </select>
                    <button type="button" class="button" id="options-search-btn"><?php esc_html_e('Search', 'wpswitchboard'); ?></button>
                </div>

                <div class="sb-module-table-wrapper">
                    <table class="sb-module-table" id="options-list">
                        <thead>
                            <tr>
                                <th><?php esc_html_e('Option Name', 'wpswitchboard'); ?></th>
                                <th><?php esc_html_e('Size', 'wpswitchboard'); ?></th>
                                <th><?php esc_html_e('Autoload', 'wpswitchboard'); ?></th>
                                <th><?php esc_html_e('Actions', 'wpswitchboard'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr class="loading-row">
                                <td colspan="4" class="loading-cell">
                                    <span class="spinner is-active"></span>
                                    <?php esc_html_e('Loading options...', 'wpswitchboard'); ?>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>

                <div class="dbcleaner-pagination" id="options-pagination"></div>
            </div>
        </div>
    </div>

    <!-- Tab Content: Metadata -->
    <div class="dbcleaner-tab-content" id="tab-metadata">
        <div class="sb-module-card">
            <div class="sb-module-card-header">
                <h2 class="sb-module-card-title"><?php esc_html_e('Post Metadata', 'wpswitchboard'); ?></h2>
                <p class="sb-module-card-desc"><?php esc_html_e('Browse metadata keys and their sizes. Identify large or unnecessary metadata.', 'wpswitchboard'); ?></p>
            </div>
            <div class="sb-module-card-body">
                <div class="dbcleaner-filters">
                    <input type="text" id="metadata-search" placeholder="<?php esc_attr_e('Search meta keys...', 'wpswitchboard'); ?>" class="regular-text">
                    <select id="metadata-sort">
                        <option value="size"><?php esc_html_e('Sort by Size', 'wpswitchboard'); ?></option>
                        <option value="key"><?php esc_html_e('Sort by Key', 'wpswitchboard'); ?></option>
                    </select>
                    <button type="button" class="button" id="metadata-search-btn"><?php esc_html_e('Search', 'wpswitchboard'); ?></button>
                </div>

                <div class="sb-module-table-wrapper">
                    <table class="sb-module-table" id="metadata-list">
                        <thead>
                            <tr>
                                <th><?php esc_html_e('Meta Key', 'wpswitchboard'); ?></th>
                                <th><?php esc_html_e('Count', 'wpswitchboard'); ?></th>
                                <th><?php esc_html_e('Total Size', 'wpswitchboard'); ?></th>
                                <th><?php esc_html_e('Actions', 'wpswitchboard'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr class="loading-row">
                                <td colspan="4" class="loading-cell">
                                    <span class="spinner is-active"></span>
                                    <?php esc_html_e('Loading metadata...', 'wpswitchboard'); ?>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>

                <div class="dbcleaner-pagination" id="metadata-pagination"></div>
            </div>
        </div>
    </div>

    <!-- Tab Content: Logs -->
    <div class="dbcleaner-tab-content" id="tab-logs">
        <div class="dbcleaner-logs-grid">
            <div class="sb-module-card">
                <div class="sb-module-card-header">
                    <h2 class="sb-module-card-title"><?php esc_html_e('Recent Activity', 'wpswitchboard'); ?></h2>
                </div>
                <div class="sb-module-card-body">
                    <div id="cleanup-log-list" class="log-list">
                        <p class="loading"><?php esc_html_e('Loading...', 'wpswitchboard'); ?></p>
                    </div>
                </div>
            </div>

            <div class="sb-module-card">
                <div class="sb-module-card-header">
                    <h2 class="sb-module-card-title"><?php esc_html_e('Database Size History', 'wpswitchboard'); ?></h2>
                </div>
                <div class="sb-module-card-body">
                    <div id="size-history-chart" class="size-chart">
                        <p class="loading"><?php esc_html_e('Loading...', 'wpswitchboard'); ?></p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Tab Content: Settings -->
    <div class="dbcleaner-tab-content" id="tab-settings">
        <!-- First Row: Schedule + Performance (2 columns) -->
        <div class="dbcleaner-settings-row">
            <!-- Schedule Settings Card -->
            <div class="sb-module-card">
                <div class="sb-module-card-header">
                    <div class="sb-module-card-header-left">
                        <h2 class="sb-module-card-title"><?php esc_html_e('Cleanup Schedule', 'wpswitchboard'); ?></h2>
                        <p class="sb-module-card-desc"><?php esc_html_e('How often to automatically clean your database.', 'wpswitchboard'); ?></p>
                    </div>
                </div>
                <div class="sb-module-card-body">
                    <div class="dbcleaner-schedule-radios">
                        <label class="dbcleaner-radio-label">
                            <input type="radio" name="auto_clean_schedule" value="disabled" <?php checked($auto_clean, 'disabled'); ?>>
                            <span><?php esc_html_e('Disabled', 'wpswitchboard'); ?></span>
                        </label>
                        <label class="dbcleaner-radio-label">
                            <input type="radio" name="auto_clean_schedule" value="daily" <?php checked($auto_clean, 'daily'); ?>>
                            <span><?php esc_html_e('Daily', 'wpswitchboard'); ?></span>
                        </label>
                        <label class="dbcleaner-radio-label">
                            <input type="radio" name="auto_clean_schedule" value="weekly" <?php checked($auto_clean, 'weekly'); ?>>
                            <span><?php esc_html_e('Weekly', 'wpswitchboard'); ?></span>
                        </label>
                    </div>
                    <p class="dbcleaner-info-text"><?php esc_html_e('Ex: Daily cleanup runs once per day via WordPress cron. Weekly runs every Sunday at midnight.', 'wpswitchboard'); ?></p>
                </div>
            </div>

            <!-- Performance Settings Card -->
            <div class="sb-module-card">
                <div class="sb-module-card-header">
                    <div class="sb-module-card-header-left">
                        <h2 class="sb-module-card-title"><?php esc_html_e('Performance Settings', 'wpswitchboard'); ?></h2>
                        <p class="sb-module-card-desc"><?php esc_html_e('Configure batch processing to prevent timeouts.', 'wpswitchboard'); ?></p>
                    </div>
                </div>
                <div class="sb-module-card-body">
                    <div class="dbcleaner-batch-row">
                        <div class="dbcleaner-batch-label">
                            <label for="setting-batch-size" class="sb-module-form-label"><?php esc_html_e('Batch Size', 'wpswitchboard'); ?></label>
                            <p class="sb-module-form-help"><?php esc_html_e('Recommended: 100 (shared), 500 (VPS), 1000 (dedicated).', 'wpswitchboard'); ?></p>
                        </div>
                        <input type="number" id="setting-batch-size" class="sb-module-input" value="<?php echo esc_attr($batch_size); ?>" min="10" max="1000" style="width: 100px;">
                    </div>
                    <p class="dbcleaner-info-text"><?php esc_html_e('Ex: With batch size 100, cleaning 500 revisions processes 100 at a time across 5 iterations to avoid timeouts.', 'wpswitchboard'); ?></p>
                </div>
            </div>
        </div>

        <!-- Items to Auto-Clean Card (Full Width) -->
        <div class="sb-module-card" style="margin-top: 20px;">
            <div class="sb-module-card-header">
                <div class="sb-module-card-header-left">
                    <h2 class="sb-module-card-title"><?php esc_html_e('Items to Auto-Clean', 'wpswitchboard'); ?></h2>
                    <p class="sb-module-card-desc"><?php esc_html_e('Select which items should be cleaned automatically. Only safe items are available for auto-cleanup.', 'wpswitchboard'); ?></p>
                </div>
            </div>
            <div class="sb-module-card-body">
                <div class="sb-module-checkbox-grid dbcleaner-items-grid">
                    <label class="sb-module-checkbox-item <?php echo in_array('revisions', $auto_clean_items) ? 'active' : ''; ?>">
                        <input type="checkbox" name="auto_clean_items[]" value="revisions" <?php checked(in_array('revisions', $auto_clean_items)); ?>>
                        <span><?php esc_html_e('Revisions', 'wpswitchboard'); ?></span>
                    </label>
                    <label class="sb-module-checkbox-item <?php echo in_array('auto_drafts', $auto_clean_items) ? 'active' : ''; ?>">
                        <input type="checkbox" name="auto_clean_items[]" value="auto_drafts" <?php checked(in_array('auto_drafts', $auto_clean_items)); ?>>
                        <span><?php esc_html_e('Auto-Drafts', 'wpswitchboard'); ?></span>
                    </label>
                    <label class="sb-module-checkbox-item <?php echo in_array('trashed_posts', $auto_clean_items) ? 'active' : ''; ?>">
                        <input type="checkbox" name="auto_clean_items[]" value="trashed_posts" <?php checked(in_array('trashed_posts', $auto_clean_items)); ?>>
                        <span><?php esc_html_e('Trashed Posts', 'wpswitchboard'); ?></span>
                    </label>
                    <label class="sb-module-checkbox-item <?php echo in_array('spam_comments', $auto_clean_items) ? 'active' : ''; ?>">
                        <input type="checkbox" name="auto_clean_items[]" value="spam_comments" <?php checked(in_array('spam_comments', $auto_clean_items)); ?>>
                        <span><?php esc_html_e('Spam Comments', 'wpswitchboard'); ?></span>
                    </label>
                    <label class="sb-module-checkbox-item <?php echo in_array('trashed_comments', $auto_clean_items) ? 'active' : ''; ?>">
                        <input type="checkbox" name="auto_clean_items[]" value="trashed_comments" <?php checked(in_array('trashed_comments', $auto_clean_items)); ?>>
                        <span><?php esc_html_e('Trashed Comments', 'wpswitchboard'); ?></span>
                    </label>
                    <label class="sb-module-checkbox-item <?php echo in_array('expired_transients', $auto_clean_items) ? 'active' : ''; ?>">
                        <input type="checkbox" name="auto_clean_items[]" value="expired_transients" <?php checked(in_array('expired_transients', $auto_clean_items)); ?>>
                        <span><?php esc_html_e('Expired Transients', 'wpswitchboard'); ?></span>
                    </label>
                    <label class="sb-module-checkbox-item <?php echo in_array('orphaned_postmeta', $auto_clean_items) ? 'active' : ''; ?>">
                        <input type="checkbox" name="auto_clean_items[]" value="orphaned_postmeta" <?php checked(in_array('orphaned_postmeta', $auto_clean_items)); ?>>
                        <span><?php esc_html_e('Orphaned Post Meta', 'wpswitchboard'); ?></span>
                    </label>
                    <label class="sb-module-checkbox-item <?php echo in_array('orphaned_commentmeta', $auto_clean_items) ? 'active' : ''; ?>">
                        <input type="checkbox" name="auto_clean_items[]" value="orphaned_commentmeta" <?php checked(in_array('orphaned_commentmeta', $auto_clean_items)); ?>>
                        <span><?php esc_html_e('Orphaned Comment Meta', 'wpswitchboard'); ?></span>
                    </label>
                </div>
            </div>
        </div>

        <!-- Save Button -->
        <div class="sb-module-form-actions" style="margin-top: 24px; justify-content: flex-end;">
            <button type="button" class="sb-module-button sb-module-button-primary" id="save-settings-btn">
                <span class="dashicons dashicons-saved" style="margin-right: 6px;"></span>
                <?php esc_html_e('Save Settings', 'wpswitchboard'); ?>
            </button>
        </div>
    </div>
    </div><!-- /.sb-module-container -->
</div>

<!-- Toast Notification -->
<div id="dbcleaner-toast" class="dbcleaner-toast">
    <span class="toast-icon"></span>
    <span class="toast-message"></span>
</div>
