<?php
if (!defined('ABSPATH')) {
    exit;
}

/* -------------------------------------------------------
   SMARTPRODUCTSEO MEDIA CLEANUP ADMIN DASHBOARD
   Version: 1.2.1
------------------------------------------------------- */

if (!function_exists('smcmlc_get_current_admin_page')) {
    function smcmlc_get_current_admin_page() {
        return isset($_GET['page']) ? sanitize_key(wp_unslash($_GET['page'])) : SMCMLC_MENU_SLUG;
    }
}

if (!function_exists('smcmlc_get_admin_tabs')) {
    function smcmlc_get_admin_tabs() {
        return array(
            array(
                'slug'  => SMCMLC_MENU_SLUG,
                'label' => '📊 Dashboard / Scan',
                'url'   => admin_url('admin.php?page=' . SMCMLC_MENU_SLUG),
            ),
            array(
                'slug'  => 'smartproductseo-media-cleanup-trash',
                'label' => '🗑️ Trash Review',
                'url'   => admin_url('admin.php?page=smartproductseo-media-cleanup-trash'),
            ),
            array(
                'slug'  => 'smartproductseo-media-cleanup-delete',
                'label' => '⚠️ Delete Permanently',
                'url'   => admin_url('admin.php?page=smartproductseo-media-cleanup-delete'),
            ),
        );
    }
}

if (!function_exists('smcmlc_is_current_admin_page')) {
    function smcmlc_is_current_admin_page($slug) {
        return smcmlc_get_current_admin_page() === $slug;
    }
}

if (!function_exists('smcmlc_get_active_button_class')) {
    function smcmlc_get_active_button_class($slug, $default_class = 'smcmlc_btn-dark') {
        return smcmlc_is_current_admin_page($slug) ? 'smcmlc_btn-pink smcmlc_tab-current' : $default_class;
    }
}

if (!function_exists('smcmlc_render_admin_tabs')) {
    function smcmlc_render_admin_tabs() {
        $current_page = smcmlc_get_current_admin_page();
        $tabs         = smcmlc_get_admin_tabs();

        echo '<nav class="smcmlc_tabs" aria-label="SmartProductSEO Media Cleanup navigation">';

        foreach ($tabs as $tab) {
            $is_active    = ($current_page === $tab['slug']);
            $active_class = $is_active ? ' smcmlc_tab-active smcmlc_tab-current smcmlc_btn-pink' : '';
            $aria_current = $is_active ? ' aria-current="page"' : '';

            echo '<a class="smcmlc_tab' . esc_attr($active_class) . '" href="' . esc_url($tab['url']) . '"' . $aria_current . '>';
            echo esc_html($tab['label']);
            echo '</a>';
        }

        echo '</nav>';
    }
}

if (!function_exists('smcmlc_admin_wrap_open')) {
    function smcmlc_admin_wrap_open($title, $subtitle = '') {
        echo '<div class="wrap smcmlc_wrap">';
        echo '<div class="smcmlc_hero">';
        echo '<div class="smcmlc_hero-glow"></div>';
        echo '<div class="smcmlc_hero-content">';
        echo '<span class="smcmlc_kicker">SmartProductSEO.com Media Safety Suite</span>';
        echo '<h1>' . esc_html($title) . '</h1>';

        if ($subtitle) {
            echo '<p>' . esc_html($subtitle) . '</p>';
        }

        echo '<div class="smcmlc_hero-actions">';
        echo '<a class="smcmlc_btn ' . esc_attr(smcmlc_get_active_button_class(SMCMLC_MENU_SLUG, 'smcmlc_btn-pink')) . '" href="' . esc_url(admin_url('admin.php?page=' . SMCMLC_MENU_SLUG)) . '">📊 Scan Media Library</a>';
        echo '<a class="smcmlc_btn ' . esc_attr(smcmlc_get_active_button_class('smartproductseo-media-cleanup-trash')) . '" href="' . esc_url(admin_url('admin.php?page=smartproductseo-media-cleanup-trash')) . '">🗑️ View Trash</a>';
        echo '<a class="smcmlc_btn ' . esc_attr(smcmlc_get_active_button_class('smartproductseo-media-cleanup-delete')) . '" href="' . esc_url(admin_url('admin.php?page=smartproductseo-media-cleanup-delete')) . '">⚠️ Permanent Delete</a>';
        echo '</div>';
        echo '</div>';
        echo '</div>';

        smcmlc_render_admin_tabs();
    }
}

if (!function_exists('smcmlc_admin_wrap_close')) {
    function smcmlc_admin_wrap_close() {
        echo '</div>';
    }
}

if (!function_exists('smcmlc_get_storage_analytics_from_last_scan')) {
    function smcmlc_get_storage_analytics_from_last_scan($limit = 5) {
        $limit     = absint($limit);
        $last_scan = get_option('smcmlc_last_scan_results', array());

        if (!$limit) {
            $limit = 5;
        }

        $analytics = array(
            'total_scan_bytes'           => 0,
            'cleanup_candidate_bytes'    => 0,
            'review_bytes'               => 0,
            'protected_bytes'            => 0,
            'likely_used_bytes'          => 0,
            'missing_file_count'         => 0,
            'broken_image_count'         => 0,
            'largest_cleanup_candidates' => array(),
            'largest_review_items'       => array(),
            'largest_protected_items'    => array(),
            'usage_breakdown'            => array(
                'WooCommerce'           => 0,
                'Theme / Logo'          => 0,
                'Content'               => 0,
                'Review'                => 0,
                'Cleanup Candidate'     => 0,
                'Broken / Missing File' => 0,
            ),
        );

        if (empty($last_scan) || !is_array($last_scan)) {
            return $analytics;
        }

        foreach ($last_scan as $item) {
            if (empty($item) || !is_array($item)) {
                continue;
            }

            $size_bytes = !empty($item['size_bytes']) ? absint($item['size_bytes']) : 0;
            $status     = !empty($item['risk_status']) ? strtolower((string) $item['risk_status']) : 'review';
            $file_name  = !empty($item['file_name']) ? sanitize_text_field($item['file_name']) : 'Unknown file';
            $used_where = !empty($item['used_where']) ? sanitize_text_field($item['used_where']) : 'Media Library';
            $edit_link  = !empty($item['edit_link']) ? esc_url_raw($item['edit_link']) : '';

            $analytics['total_scan_bytes'] += $size_bytes;

            $row = array(
                'id'         => !empty($item['id']) ? absint($item['id']) : 0,
                'file_name'  => $file_name,
                'size_bytes' => $size_bytes,
                'size'       => $size_bytes ? size_format($size_bytes) : (!empty($item['size']) ? sanitize_text_field($item['size']) : '0 B'),
                'status'     => !empty($item['risk_status']) ? sanitize_text_field($item['risk_status']) : 'Review',
                'used_where' => $used_where,
                'edit_link'  => $edit_link,
            );

            if ($status === 'cleanup candidate') {
                $analytics['cleanup_candidate_bytes'] += $size_bytes;
                $analytics['largest_cleanup_candidates'][] = $row;
                $analytics['usage_breakdown']['Cleanup Candidate']++;
            } elseif ($status === 'protected') {
                $analytics['protected_bytes'] += $size_bytes;
                $analytics['largest_protected_items'][] = $row;

                if (!empty($item['woo_usage_count'])) {
                    $analytics['usage_breakdown']['WooCommerce']++;
                } elseif (!empty($item['theme_usage_count'])) {
                    $analytics['usage_breakdown']['Theme / Logo']++;
                } else {
                    $analytics['usage_breakdown']['Review']++;
                }
            } elseif ($status === 'likely used' || $status === 'used') {
                $analytics['likely_used_bytes'] += $size_bytes;
                $analytics['usage_breakdown']['Content']++;
            } elseif ($status === 'missing file' || $status === 'broken image') {
                $analytics['review_bytes'] += $size_bytes;
                $analytics['largest_review_items'][] = $row;
                $analytics['usage_breakdown']['Broken / Missing File']++;
            } else {
                $analytics['review_bytes'] += $size_bytes;
                $analytics['largest_review_items'][] = $row;
                $analytics['usage_breakdown']['Review']++;
            }

            if (!empty($item['missing_file'])) {
                $analytics['missing_file_count']++;
            }

            if (!empty($item['broken_image'])) {
                $analytics['broken_image_count']++;
            }
        }

        $sort_by_size = function($a, $b) {
            return absint($b['size_bytes']) <=> absint($a['size_bytes']);
        };

        usort($analytics['largest_cleanup_candidates'], $sort_by_size);
        usort($analytics['largest_review_items'], $sort_by_size);
        usort($analytics['largest_protected_items'], $sort_by_size);

        $analytics['largest_cleanup_candidates'] = array_slice($analytics['largest_cleanup_candidates'], 0, $limit);
        $analytics['largest_review_items']       = array_slice($analytics['largest_review_items'], 0, $limit);
        $analytics['largest_protected_items']    = array_slice($analytics['largest_protected_items'], 0, $limit);

        return $analytics;
    }
}

if (!function_exists('smcmlc_render_storage_analytics_list')) {
    function smcmlc_render_storage_analytics_list($items, $empty_message) {
        if (empty($items) || !is_array($items)) {
            echo '<p class="smcmlc_muted">' . esc_html($empty_message) . '</p>';
            return;
        }

        echo '<div class="smcmlc_storage-list">';

        foreach ($items as $item) {
            $file_name  = !empty($item['file_name']) ? $item['file_name'] : 'Unknown file';
            $size       = !empty($item['size']) ? $item['size'] : '0 B';
            $status     = !empty($item['status']) ? $item['status'] : 'Review';
            $used_where = !empty($item['used_where']) ? $item['used_where'] : 'Media Library';
            $edit_link  = !empty($item['edit_link']) ? $item['edit_link'] : '';

            echo '<div class="smcmlc_storage-item">';
            echo '<div>';
            echo '<strong>' . esc_html($file_name) . '</strong>';
            echo '<span>' . esc_html($status) . ' · ' . esc_html($used_where) . '</span>';
            echo '</div>';
            echo '<div class="smcmlc_storage-item-meta">';
            echo '<strong>' . esc_html($size) . '</strong>';

            if ($edit_link) {
                echo '<a href="' . esc_url($edit_link) . '" target="_blank" rel="noopener noreferrer">Media Details</a>';
            }

            echo '</div>';
            echo '</div>';
        }

        echo '</div>';
    }
}

if (!function_exists('smcmlc_render_storage_analytics_dashboard')) {
    function smcmlc_render_storage_analytics_dashboard() {
        $analytics = smcmlc_get_storage_analytics_from_last_scan(5);

        echo '<div class="smcmlc_card smcmlc_storage-analytics-card">';
        echo '<div class="smcmlc_card-heading">';
        echo '<div>';
        echo '<span class="smcmlc_card-label">Storage Analytics Dashboard</span>';
        echo '<h2>Cleanup Opportunity Snapshot</h2>';
        echo '<p>Review storage totals from the last saved scan before moving anything to Trash. This stays conservative: cleanup candidates are only files with no detected WooCommerce, theme/logo, content, or parent relationship protection.</p>';
        echo '</div>';
        echo '</div>';

        echo '<div class="smcmlc_stats-grid smcmlc_storage-stats-grid">';
        echo '<div class="smcmlc_stat-card"><span>Total Scan Size</span><strong>' . esc_html(size_format(absint($analytics['total_scan_bytes']))) . '</strong><small>All images in last scan</small></div>';
        echo '<div class="smcmlc_stat-card"><span>Cleanup Opportunity</span><strong>' . esc_html(size_format(absint($analytics['cleanup_candidate_bytes']))) . '</strong><small>Candidate storage only</small></div>';
        echo '<div class="smcmlc_stat-card"><span>Review Storage</span><strong>' . esc_html(size_format(absint($analytics['review_bytes']))) . '</strong><small>Needs human review</small></div>';
        echo '<div class="smcmlc_stat-card"><span>Protected Storage</span><strong>' . esc_html(size_format(absint($analytics['protected_bytes'] + $analytics['likely_used_bytes']))) . '</strong><small>Protected or likely used</small></div>';
        echo '</div>';

        echo '<div class="smcmlc_storage-columns">';
        echo '<div class="smcmlc_storage-panel"><h3>Largest Cleanup Opportunities</h3>';
        smcmlc_render_storage_analytics_list($analytics['largest_cleanup_candidates'], 'No cleanup candidates found in the last saved scan.');
        echo '</div>';

        echo '<div class="smcmlc_storage-panel"><h3>Largest Review Items</h3>';
        smcmlc_render_storage_analytics_list($analytics['largest_review_items'], 'No review items found in the last saved scan.');
        echo '</div>';

        echo '<div class="smcmlc_storage-panel"><h3>Largest Protected Images</h3>';
        smcmlc_render_storage_analytics_list($analytics['largest_protected_items'], 'No protected images found in the last saved scan.');
        echo '</div>';
        echo '</div>';

        echo '<div class="smcmlc_used-by-breakdown">';
        echo '<h3>Used By Counter</h3>';
        echo '<div class="smcmlc_flow smcmlc_used-by-flow">';

        foreach ($analytics['usage_breakdown'] as $label => $count) {
            echo '<span>' . esc_html($label) . ': ' . esc_html(number_format_i18n(absint($count))) . '</span>';
        }

        echo '</div>';
        echo '</div>';
        echo '</div>';
    }
}

if (!function_exists('smcmlc_render_dashboard_stats')) {
    function smcmlc_render_dashboard_stats() {
        $last_scan  = get_option('smcmlc_last_scan_results', array());
        $scan_count = is_array($last_scan) ? count($last_scan) : 0;

        $protected_count          = 0;
        $likely_used_count        = 0;
        $review_count             = 0;
        $cleanup_candidate_count  = 0;
        $missing_count            = 0;
        $broken_count             = 0;
        $woo_usage_count          = 0;
        $content_usage_count      = 0;
        $theme_customizer_count   = 0;

        if (is_array($last_scan)) {
            foreach ($last_scan as $item) {
                $status = !empty($item['risk_status']) ? strtolower((string) $item['risk_status']) : '';

                if ($status === 'protected') {
                    $protected_count++;
                } elseif ($status === 'likely used') {
                    $likely_used_count++;
                } elseif ($status === 'cleanup candidate') {
                    $cleanup_candidate_count++;
                } elseif ($status === 'missing file') {
                    $missing_count++;
                } elseif ($status === 'broken image') {
                    $broken_count++;
                } elseif ($status === 'review' || $status === 'used' || $status === 'unattached') {
                    $review_count++;
                }

                if (!empty($item['woo_usage_count'])) {
                    $woo_usage_count++;
                }

                if (!empty($item['content_usage_count'])) {
                    $content_usage_count++;
                }

                if (!empty($item['theme_usage_count'])) {
                    $theme_customizer_count++;
                }

                if (!empty($item['broken_image']) && $status !== 'broken image') {
                    $broken_count++;
                }

                if (!empty($item['missing_file']) && $status !== 'missing file') {
                    $missing_count++;
                }
            }
        }

        $trash_count = 0;
        if (function_exists('smcmlc_get_trashed_images')) {
            $trash_items = smcmlc_get_trashed_images();
            $trash_count = is_array($trash_items) ? count($trash_items) : 0;
        }

        $cleanup_stats = get_option('smcmlc_cleanup_stats', array());

        if (!is_array($cleanup_stats)) {
            $cleanup_stats = array();
        }

        $images_cleaned    = !empty($cleanup_stats['images_deleted']) ? absint($cleanup_stats['images_deleted']) : 0;
        $storage_recovered = !empty($cleanup_stats['bytes_deleted']) ? size_format(absint($cleanup_stats['bytes_deleted'])) : '0 B';

        echo '<div class="smcmlc_stats-grid">';
        echo '<div class="smcmlc_stat-card"><span>Images Scanned</span><strong>' . esc_html(number_format_i18n($scan_count)) . '</strong><small>Last saved scan</small></div>';
        echo '<div class="smcmlc_stat-card"><span>Protected Images</span><strong>' . esc_html(number_format_i18n($protected_count)) . '</strong><small>WooCommerce, logo, or theme protected</small></div>';
        echo '<div class="smcmlc_stat-card"><span>Theme / Logo Usage</span><strong>' . esc_html(number_format_i18n($theme_customizer_count)) . '</strong><small>Customizer, header, footer, theme options</small></div>';
        echo '<div class="smcmlc_stat-card"><span>Likely Used</span><strong>' . esc_html(number_format_i18n($likely_used_count)) . '</strong><small>Found inside content</small></div>';
        echo '<div class="smcmlc_stat-card"><span>WooCommerce Usage</span><strong>' . esc_html(number_format_i18n($woo_usage_count)) . '</strong><small>Product/gallery/variation</small></div>';
        echo '<div class="smcmlc_stat-card"><span>Used In Content</span><strong>' . esc_html(number_format_i18n($content_usage_count)) . '</strong><small>Page/post/product body</small></div>';
        echo '<div class="smcmlc_stat-card"><span>Review Items</span><strong>' . esc_html(number_format_i18n($review_count)) . '</strong><small>Check before trash</small></div>';
        echo '<div class="smcmlc_stat-card"><span>Cleanup Candidates</span><strong>' . esc_html(number_format_i18n($cleanup_candidate_count)) . '</strong><small>No detected usage</small></div>';
        echo '<div class="smcmlc_stat-card"><span>Missing Files</span><strong>' . esc_html(number_format_i18n($missing_count)) . '</strong><small>Database record only</small></div>';
        echo '<div class="smcmlc_stat-card"><span>Broken Images</span><strong>' . esc_html(number_format_i18n($broken_count)) . '</strong><small>Missing thumbnails/sizes</small></div>';
        echo '<div class="smcmlc_stat-card"><span>Images In Trash</span><strong>' . esc_html(number_format_i18n($trash_count)) . '</strong><small>Restore or delete</small></div>';
        echo '<div class="smcmlc_stat-card"><span>Images Cleaned</span><strong>' . esc_html(number_format_i18n($images_cleaned)) . '</strong><small>Permanently deleted</small></div>';
        echo '<div class="smcmlc_stat-card"><span>Storage Recovered</span><strong>' . esc_html($storage_recovered) . '</strong><small>Estimated cleanup total</small></div>';
        echo '</div>';
    }
}


if (!function_exists('smcmlc_render_upgrade_to_pro_card')) {
    function smcmlc_render_upgrade_to_pro_card() {
        $upgrade_url = 'https://smartproductseo.com/product/smart-media-cleanup-pro-for-wordpress/';

        echo '<div class="smcmlc_card smcmlc_safety-card smcmlc_upgrade-card">';
        echo '<span class="smcmlc_card-label">SmartProductSEO PRO Upgrade</span>';
        echo '<h2>Unlock Smart Media Cleanup PRO™</h2>';
        echo '<p>Upgrade when you are ready for deeper media relationship detection, advanced cleanup intelligence, builder-aware scanning, confidence scoring, and premium cleanup reports for larger WordPress and WooCommerce websites.</p>';
        echo '<div class="smcmlc_flow">';
        echo '<span>Deep Database Scanner</span><span>Builder Detection</span><span>Theme Option Scanner</span><span>Confidence Scoring</span><span>Advanced Reports</span>';
        echo '</div>';
        echo '<p><a class="smcmlc_btn smcmlc_btn-pink smcmlc_btn-large" href="' . esc_url($upgrade_url) . '" target="_blank" rel="noopener noreferrer">🔑 Upgrade To PRO</a></p>';
        echo '</div>';
    }
}

if (!function_exists('smcmlc_render_admin_dashboard')) {
    function smcmlc_render_admin_dashboard() {
        smcmlc_admin_wrap_open(
            'SmartProductSEO Media Cleanup',
            'Scan Media Library images with conservative safety rules, detect WooCommerce featured/gallery/variation usage, site logo usage, theme option image usage, real page/post/product content usage, broken files, missing files, and true cleanup candidates before anything is moved to Trash.'
        );

        smcmlc_render_dashboard_stats();
        smcmlc_render_storage_analytics_dashboard();

        echo '<div class="smcmlc_card smcmlc_scan-card">';
        echo '<div class="smcmlc_card-heading smcmlc_scan-heading">';
        echo '<div>';
        echo '<span class="smcmlc_card-label">Media Library Scanner</span>';
        echo '<h2>Dashboard / Scan</h2>';
        echo '<p>Start with a safe Media Library scan. The scanner protects direct WooCommerce relationships, site logo usage, header logo usage, footer logo usage, theme option image settings, and real content usage. It separates Broken Images and Missing Files from true cleanup candidates.</p>';
        echo '</div>';
        echo '<div class="smcmlc_scan-controls">';
        echo '<span class="smcmlc_scan-note">Scans all image attachments.</span>';
        echo '<button class="smcmlc_btn smcmlc_btn-pink smcmlc_btn-large" id="smcmlc_run-scan" type="button">Scan Media Library</button>';
        echo '</div>';
        echo '</div>';
        echo '</div>';

        echo '<div class="smcmlc_card smcmlc_results-card">';
        echo '<div class="smcmlc_card-heading">';
        echo '<div>';
        echo '<span class="smcmlc_card-label">Visual Review Table</span>';
        echo '<h2>Scan Results</h2>';
        echo '<p>Each result shows image preview, file name, attached location, Used In details, file health notes, risk status, file size, selectable checkbox, and safe cleanup actions.</p>';
        echo '</div>';
        echo '<div class="smcmlc_bulk-actions">';
        echo '<button type="button" class="smcmlc_btn smcmlc_btn-pink" id="smcmlc_bulk-trash-selected">Move Selected to Trash</button>';
        echo '<span class="smcmlc_bulk-note">Select images from the table after scanning.</span>';
        echo '</div>';
        echo '</div>';

        echo '<div class="smcmlc_notice smcmlc_warning smcmlc_results-warning">';
        echo '<strong>Safety reminder:</strong> Cleanup Candidate means no strong WooCommerce usage, no site logo usage, no header/footer logo usage, no theme option usage, no real page/post/product content usage, and no protected relationship was detected by the scanner. Trash first, check products/pages, and restore anything that causes a broken image.';
        echo '</div>';

        echo '<div id="smcmlc_scan-results" class="smcmlc_results-white"><p>No scan has been run yet. Click <strong>Scan Media Library</strong> to start reviewing images.</p></div>';
        echo '</div>';

        echo '<div class="smcmlc_card smcmlc_safety-card">';
        echo '<h2>Recommended Safety Flow</h2>';
        echo '<div class="smcmlc_flow">';
        echo '<span>Scan</span><span>Protect WooCommerce Usage</span><span>Protect Site Logo</span><span>Protect Header/Footer Logos</span><span>Protect Theme Options</span><span>Find Real Used In Locations</span><span>Flag Broken/Missing Files</span><span>Review Unclear Items</span><span>Select True Candidates</span><span>Move to Trash</span><span>Check Page</span><span>Restore or Delete</span>';
        echo '</div>';
        echo '</div>';

        echo '<div class="smcmlc_card smcmlc_safety-card">';
        echo '<h2>Trash Review Power Tools</h2>';
        echo '<p>Use Select All, Restore Selected, Permanent Delete Selected, storage recovered totals, and images cleaned counters after moving suspected unused images to Trash.</p>';
        echo '<div class="smcmlc_flow">';
        echo '<span>Trash First</span><span>Select All If Safe</span><span>Restore Selected</span><span>Permanent Delete Selected</span><span>Track Storage Recovered</span>';
        echo '</div>';
        echo '</div>';

        smcmlc_render_upgrade_to_pro_card();

        smcmlc_admin_wrap_close();
    }
}

if (!function_exists('smcmlc_render_trash_review_page')) {
    function smcmlc_render_trash_review_page() {
        smcmlc_admin_wrap_open(
            'Trash Review',
            'Images in Trash are not permanently deleted yet. Use Select All, Restore Selected, or Permanent Delete Selected after reviewing your site.'
        );

        echo '<div class="smcmlc_notice smcmlc_warning">';
        echo '<strong>Trash Review Safety Notice:</strong> Trash does not permanently remove images. Use Restore Selected for anything that still appears on your site, and only use Permanent Delete Selected after checking your pages/products.';
        echo '</div>';

        echo '<div class="smcmlc_card smcmlc_results-card">';
        echo '<div class="smcmlc_card-heading">';
        echo '<div>';
        echo '<span class="smcmlc_card-label">Free Media Library Trash</span>';
        echo '<h2>Media Library Trash Review</h2>';
        echo '<p>Review trashed Media Library images with bulk restore, bulk permanent delete, select all controls, trash size totals, storage recovered stats, and image cleanup counters.</p>';
        echo '</div>';
        echo '<a class="smcmlc_btn smcmlc_btn-dark" href="' . esc_url(admin_url('admin.php?page=smartproductseo-media-cleanup-delete')) . '">⚠️ Go to Permanent Delete</a>';
        echo '</div>';
        echo '<div id="smcmlc_trash-results" class="smcmlc_results-white">';

        if (function_exists('smcmlc_render_trash_table')) {
            smcmlc_render_trash_table(false);
        } else {
            echo '<p>Trash manager is not loaded yet.</p>';
        }

        echo '</div>';
        echo '</div>';

        smcmlc_admin_wrap_close();
    }
}

if (!function_exists('smcmlc_render_delete_permanently_page')) {
    function smcmlc_render_delete_permanently_page() {
        smcmlc_admin_wrap_open(
            'Delete Permanently',
            'Final review before permanent deletion. Use Select All carefully because permanent deletion removes the attachment record and image files from the server.'
        );

        echo '<div class="smcmlc_notice smcmlc_danger">';
        echo '<strong>Permanent Delete Warning:</strong> Permanent deletion cannot be undone. Use bulk delete only after reviewing the site and confirming the images are no longer needed.';
        echo '</div>';

        echo '<div class="smcmlc_card smcmlc_results-card">';
        echo '<div class="smcmlc_card-heading">';
        echo '<div>';
        echo '<span class="smcmlc_card-label">Final Cleanup Step</span>';
        echo '<h2>Media Library Permanent Cleanup</h2>';
        echo '<p>Only permanently delete images after checking the site. This page supports Select All and Permanent Delete Selected for faster final cleanup.</p>';
        echo '</div>';
        echo '<a class="smcmlc_btn smcmlc_btn-dark" href="' . esc_url(admin_url('admin.php?page=smartproductseo-media-cleanup-trash')) . '">🗑️ Back to Trash Review</a>';
        echo '</div>';
        echo '<div class="smcmlc_results-white">';

        if (function_exists('smcmlc_render_trash_table')) {
            smcmlc_render_trash_table(true);
        } else {
            echo '<p>Trash manager is not loaded yet.</p>';
        }

        echo '</div>';
        echo '</div>';

        smcmlc_admin_wrap_close();
    }
}