<?php
/**
 * Comment Image Boost Admin
 * PÃƒÂ¡gina de configuraÃƒÂ§ÃƒÂµes no admin do WordPress
 *
 * @package CommentImageBoost
 * @author AuditySEO
 * @link https://www.audityseo.com/
 * @version 1.0.16
 */

if (!defined('ABSPATH')) {
    die('You are not allowed to call this page directly.');
}

/**
 * Classe para gerenciar configuraÃƒÂ§ÃƒÂµes no admin
 */
class CIBOOST_Admin {

    /**
     * ID da opÃƒÂ§ÃƒÂ£o no banco de dados
     */
    const OPTION_ID = 'ciboost_comment_images';

    /**
     * Hook suffix for the plugin settings page.
     *
     * @var string
     */
    private $admin_page_hook = '';

    /**
     * Construtor
     */
    public function __construct() {
        try {
            add_action('admin_menu', array($this, 'add_admin_menu'));
            add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_assets'));
            add_action('admin_init', array($this, 'register_settings'));
        } catch (Exception $e) {
            ciboost_log('Error in CIBOOST_Admin constructor: ' . $e->getMessage());
        }
    }

    private function get_upgrade_url() {
        if (!function_exists('ca_fs')) {
            return '#';
        }

        $fs = ca_fs();

        if ($fs && method_exists($fs, 'get_upgrade_url')) {
            return $fs->get_upgrade_url();
        }

        return '#';
    }

    /**
     * Checks whether Freemius is waiting for email confirmation.
     *
     * @return bool
     */
    private function has_pending_freemius_email_confirmation() {
        if (!function_exists('ca_fs')) {
            return false;
        }

        $fs = ca_fs();
        return $fs && method_exists($fs, 'is_pending_activation') && $fs->is_pending_activation();
    }

    /**
     * Gets a URL that opens the Freemius opt-in flow again.
     *
     * @return string
     */
    private function get_freemius_opt_in_url() {
        if (!function_exists('ca_fs')) {
            return admin_url('admin.php?page=comment-image-boost');
        }

        $fs = ca_fs();
        if ($fs && method_exists($fs, 'get_activation_url')) {
            return $fs->get_activation_url(array(
                'fs_action' => 'reset_pending_activation_mode',
                'fs_unique_affix' => 'comment-image-boost',
            ));
        }

        return add_query_arg(
            array(
                'page' => 'comment-image-boost',
                'fs_action' => 'reset_pending_activation_mode',
                'fs_unique_affix' => 'comment-image-boost',
            ),
            admin_url('admin.php')
        );
    }

    /**
     * Renders a friendly card for pending Freemius email confirmation.
     */
    private function render_pending_email_card() {
        if (!$this->has_pending_freemius_email_confirmation()) {
            return;
        }

        $opt_in_url = $this->get_freemius_opt_in_url();
        $refresh_url = admin_url('admin.php?page=comment-image-boost');
        ?>
        <div class="ciboost-pending-email-card">
            <div class="ciboost-pending-email-icon" aria-hidden="true">
                <span class="dashicons dashicons-email-alt"></span>
            </div>
            <div class="ciboost-pending-email-content">
                <h2><?php esc_html_e('One last step: confirm your email', 'comment-image-boost'); ?></h2>
                <p>
                    <?php esc_html_e('Comment Image Boost is ready to use, but Freemius is still waiting for your email confirmation to finish connecting this site.', 'comment-image-boost'); ?>
                    <?php esc_html_e('Please check your inbox and spam folder for the confirmation email.', 'comment-image-boost'); ?>
                </p>
                <p class="ciboost-pending-email-note">
                    <?php esc_html_e('If you did not receive the email, you can start the opt-in flow again and request a new confirmation.', 'comment-image-boost'); ?>
                </p>
                <div class="ciboost-pending-email-actions">
                    <a class="button button-primary" href="<?php echo esc_url($opt_in_url); ?>">
                        <?php esc_html_e('Start opt-in again', 'comment-image-boost'); ?>
                    </a>
                    <a class="button" href="<?php echo esc_url($refresh_url); ?>">
                        <?php esc_html_e('I confirmed, refresh status', 'comment-image-boost'); ?>
                    </a>
                </div>
            </div>
        </div>
        <?php
    }

    /**
     * Enqueue admin CSS/JS for the plugin settings screen.
     *
     * @param string $hook Current admin page hook.
     */
    public function enqueue_admin_assets($hook) {
        if ($hook !== $this->admin_page_hook) {
            return;
        }

        $version = defined('CIBOOST_VERSION') ? CIBOOST_VERSION : '1.0.16';

        wp_enqueue_style('dashicons');
        wp_enqueue_style(
            'ciboost-admin',
            CIBOOST_PLUGIN_URL . 'assets/css/admin.css',
            array(),
            $version
        );

        wp_enqueue_script(
            'ciboost-admin',
            CIBOOST_PLUGIN_URL . 'assets/js/admin.js',
            array(),
            $version,
            true
        );
        wp_add_inline_script(
            'ciboost-admin',
            'window.ciboostAdmin = ' . wp_json_encode(
                array(
                    'isFreeUser' => !ciboost_can_use_premium_features(),
                )
            ) . ';',
            'before'
        );

        if (!ciboost_can_use_premium_features()) {
            wp_enqueue_style(
                'ciboost-upgrade-modal',
                CIBOOST_PLUGIN_URL . 'assets/css/upgrade-modal.css',
                array('ciboost-admin'),
                $version
            );

            wp_enqueue_script(
                'ciboost-upgrade-modal',
                CIBOOST_PLUGIN_URL . 'assets/js/upgrade-modal.js',
                array('ciboost-admin'),
                $version,
                true
            );

            wp_add_inline_script(
                'ciboost-upgrade-modal',
                'window.ciboostUpgradeModal = ' . wp_json_encode(
                    array(
                        'features' => function_exists('ciboost_get_upgrade_modal_feature_config') ? ciboost_get_upgrade_modal_feature_config() : array(),
                    )
                ) . ';',
                'before'
            );
        }
    }

    /**
     * Adiciona menu no admin do WordPress
     */
    public function add_admin_menu() {
        try {
            $this->admin_page_hook = add_menu_page(
                __('Comment Image Boost', 'comment-image-boost'),
                __('Comment Images', 'comment-image-boost'),
                'manage_options',
                'comment-image-boost',
                array($this, 'render_settings_page'),
                CIBOOST_PLUGIN_URL . 'assets/logo-menu.png',
                58
            );

        } catch (Exception $e) {
            ciboost_log('Error adding admin menu: ' . $e->getMessage());
        }
    }

    /**
     * Registra configuraÃƒÂ§ÃƒÂµes
     */
    public function register_settings() {
        try {
            register_setting(
                self::OPTION_ID,
                self::OPTION_ID,
                array($this, 'sanitize_settings')
            );

            // --- Tab 1: Upload ---
            add_settings_section('ciboost_section_upload', __('Upload Settings', 'comment-image-boost'), array($this, 'render_section_upload'), 'ciboost_tab_upload');

            add_settings_field('enabled', __('Enable Comment Images', 'comment-image-boost'), array($this, 'render_field_checkbox'), 'ciboost_tab_upload', 'ciboost_section_upload', array(
                'name' => 'enabled',
                'desc' => __('Enable image upload functionality in comments.', 'comment-image-boost')
            ));
            add_settings_field('required', __('Required Image', 'comment-image-boost'), array($this, 'render_field_checkbox'), 'ciboost_tab_upload', 'ciboost_section_upload', array(
                'name' => 'required',
                'desc' => __('Make image upload required for commenting.', 'comment-image-boost'),
                'pro' => true
            ));
            add_settings_field('allow_multiple', __('Allow Multiple Images', 'comment-image-boost'), array($this, 'render_field_checkbox'), 'ciboost_tab_upload', 'ciboost_section_upload', array(
                'name' => 'allow_multiple',
                'desc' => __('Allow users to upload multiple images per comment.', 'comment-image-boost'),
                'pro' => true
            ));
            add_settings_field('max_size_mb', __('Maximum File Size (MB)', 'comment-image-boost'), array($this, 'render_field_number'), 'ciboost_tab_upload', 'ciboost_section_upload', array(
                'name' => 'max_size_mb',
                'desc' => __('Maximum image file size in megabytes.', 'comment-image-boost'),
                'min' => 1, 'max' => 50
            ));
            add_settings_field('allowed_formats', __('Allowed Image Formats', 'comment-image-boost'), array($this, 'render_field_formats'), 'ciboost_tab_upload', 'ciboost_section_upload', array(
                'name' => 'allowed_formats',
                'desc' => __('Select which image formats are allowed.', 'comment-image-boost')
            ));
            add_settings_field('strip_metadata', __('Remove EXIF/GPS Metadata', 'comment-image-boost'), array($this, 'render_field_checkbox'), 'ciboost_tab_upload', 'ciboost_section_upload', array(
                'name' => 'strip_metadata',
                'default' => true,
                'desc' => __('Re-save uploaded images to explicitly remove embedded EXIF/GPS metadata.', 'comment-image-boost')
            ));
            add_settings_field('save_to_media_library', __('Save to Media Library', 'comment-image-boost'), array($this, 'render_field_checkbox'), 'ciboost_tab_upload', 'ciboost_section_upload', array(
                'name' => 'save_to_media_library',
                'desc' => __('Also register uploaded comment images in the WordPress Media Library and generate real WordPress thumbnails.', 'comment-image-boost')
            ));

            // --- Tab 2: Display ---
            add_settings_section('ciboost_section_display', __('Display Settings', 'comment-image-boost'), array($this, 'render_section_display'), 'ciboost_tab_display');

            add_settings_field('image_size', __('Display Image Size', 'comment-image-boost'), array($this, 'render_field_image_size'), 'ciboost_tab_display', 'ciboost_section_display', array(
                'name' => 'image_size',
                'desc' => __('Image size to display in comments.', 'comment-image-boost')
            ));
            add_settings_field('preview_size', __('Preview Size (px)', 'comment-image-boost'), array($this, 'render_field_number'), 'ciboost_tab_display', 'ciboost_section_display', array(
                'name' => 'preview_size',
                'desc' => __('Preview thumbnail size in pixels (before upload).', 'comment-image-boost'),
                'min' => 80, 'max' => 300
            ));
            add_settings_field('display_layout', __('Display Layout', 'comment-image-boost'), array($this, 'render_field_layout'), 'ciboost_tab_display', 'ciboost_section_display', array(
                'name' => 'display_layout',
                'desc' => __('How images should be displayed in comments.', 'comment-image-boost')
            ));
            add_settings_field('image_position', __('Image Position', 'comment-image-boost'), array($this, 'render_field_position'), 'ciboost_tab_display', 'ciboost_section_display', array(
                'name' => 'image_position',
                'desc' => __('Choose whether images appear before or after the comment text.', 'comment-image-boost')
            ));
            add_settings_field('enable_lightbox', __('Lightbox Viewer', 'comment-image-boost'), array($this, 'render_field_checkbox'), 'ciboost_tab_display', 'ciboost_section_display', array(
                'name' => 'enable_lightbox',
                'desc' => __('Open comment images in a modal gallery instead of a new browser tab.', 'comment-image-boost')
            ));
            add_settings_field('show_badge', __('Photo Comment Badge', 'comment-image-boost'), array($this, 'render_field_checkbox'), 'ciboost_tab_display', 'ciboost_section_display', array(
                'name' => 'show_badge',
                'default' => true,
                'desc' => __('Show a visual "Photo comment" badge on public comment images and galleries.', 'comment-image-boost')
            ));
            add_settings_field('lazy_loading', __('Lazy Loading', 'comment-image-boost'), array($this, 'render_field_checkbox'), 'ciboost_tab_display', 'ciboost_section_display', array(
                'name' => 'lazy_loading',
                'desc' => __('Enable lazy loading for comment images to improve page performance.', 'comment-image-boost')
            ));

            // --- Tab 3: Pro Features ---
            // Only register Pro fields and compression settings for premium users.
            // Free users see only the locked overlay â€” no real inputs are rendered.
            if (ciboost_can_use_premium_features()) {
                add_settings_section('ciboost_section_pro', __('Pro Features', 'comment-image-boost'), array($this, 'render_section_pro'), 'ciboost_tab_pro');

                add_settings_field('enable_captions', __('Image Captions', 'comment-image-boost'), array($this, 'render_field_checkbox'), 'ciboost_tab_pro', 'ciboost_section_pro', array(
                    'name' => 'enable_captions',
                    'desc' => __('Allow visitors and moderators to add short captions to uploaded images.', 'comment-image-boost')
                ));
                add_settings_field('gallery_columns', __('Gallery Columns', 'comment-image-boost'), array($this, 'render_field_number'), 'ciboost_tab_pro', 'ciboost_section_pro', array(
                    'name' => 'gallery_columns',
                    'desc' => __('Default number of columns for shortcode galleries.', 'comment-image-boost'),
                    'default' => 4, 'min' => 1, 'max' => 6
                ));
                add_settings_field('gallery_layout', __('Gallery Layout', 'comment-image-boost'), array($this, 'render_field_gallery_layout'), 'ciboost_tab_pro', 'ciboost_section_pro', array(
                    'name' => 'gallery_layout',
                    'desc' => __('Default layout for galleries rendered by shortcode, Gutenberg block, or Elementor widget.', 'comment-image-boost')
                ));
                add_settings_field('enabled_post_types', __('Enabled Post Types', 'comment-image-boost'), array($this, 'render_field_post_types'), 'ciboost_tab_pro', 'ciboost_section_pro', array(
                    'name' => 'enabled_post_types',
                    'desc' => __('Leave empty to enable comment images on every public post type with comments.', 'comment-image-boost')
                ));
                add_settings_field('enabled_categories', __('Enabled Categories', 'comment-image-boost'), array($this, 'render_field_categories'), 'ciboost_tab_pro', 'ciboost_section_pro', array(
                    'name' => 'enabled_categories',
                    'desc' => __('Optional: restrict image uploads to selected post categories.', 'comment-image-boost')
                ));

                // --- Compression (separate option group, rendered inside Pro tab) ---
                register_setting('ciboost_image_compression', 'ciboost_image_compression', array($this, 'sanitize_compression_settings'));

                add_settings_section('ciboost_image_compression_section', __('Image Compression', 'comment-image-boost'), array($this, 'render_section_compression'), 'ciboost_image_compression');

                add_settings_field('compression_enabled', __('Enable Image Compression', 'comment-image-boost'), array($this, 'render_field_compression_checkbox'), 'ciboost_image_compression', 'ciboost_image_compression_section', array(
                    'name' => 'enabled',
                    'desc' => __('Automatically compress and resize images before saving to server.', 'comment-image-boost')
                ));
                add_settings_field('compression_quality', __('Compression Quality (%)', 'comment-image-boost'), array($this, 'render_field_compression_quality'), 'ciboost_image_compression', 'ciboost_image_compression_section', array(
                    'name' => 'quality',
                    'desc' => __('Image quality after compression (0-100). Higher values = better quality but larger files. Recommended: 80-85.', 'comment-image-boost'),
                    'min' => 60, 'max' => 100
                ));
                add_settings_field('compression_max_width', __('Maximum Width (px)', 'comment-image-boost'), array($this, 'render_field_compression_number'), 'ciboost_image_compression', 'ciboost_image_compression_section', array(
                    'name' => 'max_width',
                    'desc' => __('Images will be resized to this width maintaining aspect ratio. Default: 767px.', 'comment-image-boost'),
                    'min' => 300, 'max' => 2000
                ));
                add_settings_field('compression_progressive', __('Progressive Images', 'comment-image-boost'), array($this, 'render_field_compression_checkbox'), 'ciboost_image_compression', 'ciboost_image_compression_section', array(
                    'name' => 'progressive',
                    'desc' => __('Enable progressive JPEG and interlaced PNG for better perceived loading speed.', 'comment-image-boost')
                ));
            }

        } catch (Exception $e) {
            ciboost_log('Error registering settings: ' . $e->getMessage());
        }
    }

    public function render_section_upload() {
        echo '<p>' . esc_html__('Configure file upload rules and storage options.', 'comment-image-boost') . '</p>';
    }

    public function render_section_display() {
        echo '<p>' . esc_html__('Control how images appear in comments and galleries.', 'comment-image-boost') . '</p>';
    }

    public function render_section_pro() {
        if (!ciboost_can_use_premium_features()) {
            echo '<p class="description"><strong>' . esc_html__('Pro growth tools:', 'comment-image-boost') . '</strong> ';
            echo esc_html__('multiple images, required photo comments, WebP/AVIF support, higher upload limits, carousel galleries, and automatic compression.', 'comment-image-boost');
            echo '</p>';
        }
    }

    /**
     * Renderiza descriÃƒÂ§ÃƒÂ£o da seÃƒÂ§ÃƒÂ£o de compressÃƒÂ£o
     */
    public function render_section_compression() {
        try {
            echo '<p>' . esc_html__('Configure automatic image compression and resizing to optimize storage and loading speed.', 'comment-image-boost') . '</p>';
            if (!ciboost_can_use_premium_features()) {
                echo '<p class="description"><strong>' . esc_html__('Pro feature.', 'comment-image-boost') . '</strong> ';
                echo esc_html__('Compression settings are saved only after a valid Pro license is active.', 'comment-image-boost');
                echo '</p>';
            }
            echo '<p style="background: #fff3cd; border-left: 4px solid #ffc107; padding: 12px; margin-top: 15px;">';
            echo '<strong>' . esc_html__('Note:', 'comment-image-boost') . '</strong> ';
            echo esc_html__('Compression is applied automatically after images are uploaded. This helps reduce file sizes by up to 70% while maintaining visual quality.', 'comment-image-boost');
            echo '</p>';
        } catch (Exception $e) {
            ciboost_log('Error rendering compression section: ' . $e->getMessage());
        }
    }

    /**
     * Renderiza campo checkbox para compressÃƒÂ£o
     */
    public function render_field_compression_checkbox($args) {
        try {
            $options = get_option('ciboost_image_compression', array());
            $name = $args['name'];
            $value = isset($options[$name]) ? $options[$name] : true;
            $desc = isset($args['desc']) ? $args['desc'] : '';

            ?>
            <input type="hidden" name="ciboost_image_compression[<?php echo esc_attr($name); ?>]" value="0">
            <label>
                <input
                    type="checkbox"
                    name="ciboost_image_compression[<?php echo esc_attr($name); ?>]"
                    value="1"
                    <?php checked(1, $value); ?>
                />
                <?php echo esc_html($desc); ?>
            </label>
            <?php

        } catch (Exception $e) {
            ciboost_log('Error rendering compression checkbox field: ' . $e->getMessage());
        }
    }

    /**
     * Renderiza campo de qualidade de compressÃƒÂ£o
     */
    public function render_field_compression_quality($args) {
        try {
            $options = get_option('ciboost_image_compression', array());
            $name = $args['name'];
            $value = isset($options[$name]) ? $options[$name] : 85;
            $desc = isset($args['desc']) ? $args['desc'] : '';
            $min = isset($args['min']) ? $args['min'] : 60;
            $max = isset($args['max']) ? $args['max'] : 100;

            ?>
            <input
                type="range"
                name="ciboost_image_compression[<?php echo esc_attr($name); ?>]"
                value="<?php echo esc_attr($value); ?>"
                min="<?php echo esc_attr($min); ?>"
                max="<?php echo esc_attr($max); ?>"
                class="ciboost-quality-slider"
                oninput="this.nextElementSibling.value = this.value"
            />
            <output class="ciboost-quality-value"><?php echo esc_html($value); ?></output>
            <?php if ($desc): ?>
                <p class="description"><?php echo esc_html($desc); ?></p>
            <?php endif; ?>
            <?php

        } catch (Exception $e) {
            ciboost_log('Error rendering compression quality field: ' . $e->getMessage());
        }
    }

    /**
     * Renderiza campo numÃƒÂ©rico para compressÃƒÂ£o
     */
    public function render_field_compression_number($args) {
        try {
            $options = get_option('ciboost_image_compression', array());
            $name = $args['name'];
            $value = isset($options[$name]) ? $options[$name] : 767;
            $desc = isset($args['desc']) ? $args['desc'] : '';
            $min = isset($args['min']) ? $args['min'] : 1;
            $max = isset($args['max']) ? $args['max'] : 2000;

            ?>
            <input
                type="number"
                name="ciboost_image_compression[<?php echo esc_attr($name); ?>]"
                value="<?php echo esc_attr($value); ?>"
                min="<?php echo esc_attr($min); ?>"
                max="<?php echo esc_attr($max); ?>"
                class="small-text"
            />
            <?php if ($desc): ?>
                <p class="description"><?php echo esc_html($desc); ?></p>
            <?php endif; ?>
            <?php

        } catch (Exception $e) {
            ciboost_log('Error rendering compression number field: ' . $e->getMessage());
        }
    }

    /**
     * Sanitiza configuraÃƒÂ§ÃƒÂµes de compressÃƒÂ£o antes de salvar
     */
    public function sanitize_compression_settings($input) {
        try {
            if (!ciboost_can_use_premium_features()) {
                return array(
                    'enabled' => false,
                    'quality' => 85,
                    'max_width' => 767,
                    'progressive' => true,
                );
            }

            $sanitized = array();

            if (isset($input['enabled'])) {
                $sanitized['enabled'] = (bool) $input['enabled'];
            }

            if (isset($input['quality'])) {
                $sanitized['quality'] = absint($input['quality']);
                if ($sanitized['quality'] < 60) {
                    $sanitized['quality'] = 60;
                }
                if ($sanitized['quality'] > 100) {
                    $sanitized['quality'] = 100;
                }
            }

            if (isset($input['max_width'])) {
                $sanitized['max_width'] = absint($input['max_width']);
                if ($sanitized['max_width'] < 300) {
                    $sanitized['max_width'] = 300;
                }
                if ($sanitized['max_width'] > 2000) {
                    $sanitized['max_width'] = 2000;
                }
            }

            if (isset($input['progressive'])) {
                $sanitized['progressive'] = (bool) $input['progressive'];
            }

            return $sanitized;

        } catch (Exception $e) {
            ciboost_log('Error sanitizing compression settings: ' . $e->getMessage());
            return array();
        }
    }

    /**
     * Renderiza campo checkbox
     */
    public function render_field_checkbox($args) {
        try {
            $options = get_option(self::OPTION_ID, array());
            $name = $args['name'];
            $is_pro = !empty($args['pro']) && !ciboost_can_use_premium_features();
            $value = isset($options[$name]) ? $options[$name] : (isset($args['default']) ? $args['default'] : false);
            $desc = isset($args['desc']) ? $args['desc'] : '';

            if ($is_pro) {
                // Visual-only: no name attribute, no hidden input â€” nothing is submitted.
                ?>
                <label class="ciboost-field-pro-locked">
                    <input type="checkbox" disabled />
                    <?php echo esc_html($desc); ?>
                    <span class="ciboost-pro-badge">PRO</span>
                </label>
                <?php
                return;
            }

            ?>
            <input type="hidden" name="<?php echo esc_attr(self::OPTION_ID . '[' . $name . ']'); ?>" value="0">
            <label>
                <input
                    type="checkbox"
                    name="<?php echo esc_attr(self::OPTION_ID . '[' . $name . ']'); ?>"
                    value="1"
                    <?php checked(1, $value); ?>
                />
                <?php echo esc_html($desc); ?>
            </label>
            <?php

        } catch (Exception $e) {
            ciboost_log('Error rendering checkbox field: ' . $e->getMessage());
        }
    }

    /**
     * Renderiza campo numÃƒÂ©rico
     */
    public function render_field_number($args) {
        try {
            $options = get_option(self::OPTION_ID, array());
            $name = $args['name'];
            $value = isset($options[$name]) ? $options[$name] : (isset($args['default']) ? $args['default'] : 2);
            $desc = isset($args['desc']) ? $args['desc'] : '';
            $min = isset($args['min']) ? $args['min'] : 1;
            $max = isset($args['max']) ? $args['max'] : 10;

            ?>
            <input
                type="number"
                name="<?php echo esc_attr(self::OPTION_ID . '[' . $name . ']'); ?>"
                value="<?php echo esc_attr($value); ?>"
                min="<?php echo esc_attr($min); ?>"
                max="<?php echo esc_attr($max); ?>"
                class="small-text"
            />
            <?php if ($desc): ?>
                <p class="description"><?php echo esc_html($desc); ?></p>
            <?php endif; ?>
            <?php

        } catch (Exception $e) {
            ciboost_log('Error rendering number field: ' . $e->getMessage());
        }
    }

    /**
     * Renderiza campo de tamanho de imagem
     */
    public function render_field_image_size($args) {
        try {
            $options = get_option(self::OPTION_ID, array());
            $name = $args['name'];
            $value = isset($options[$name]) ? $options[$name] : 'medium';
            $desc = isset($args['desc']) ? $args['desc'] : '';

            $sizes = $this->get_image_sizes();

            ?>
            <select name="<?php echo esc_attr(self::OPTION_ID . '[' . $name . ']'); ?>">
                <?php foreach ($sizes as $size_name => $size_info): ?>
                    <option value="<?php echo esc_attr($size_name); ?>" <?php selected($size_name, $value); ?>>
                        <?php
                        echo esc_html(ucfirst($size_name));
                        if (isset($size_info['width']) && isset($size_info['height'])) {
                            echo ' (' . esc_html(absint($size_info['width'])) . 'x' . esc_html(absint($size_info['height'])) . ')';
                        }
                        ?>
                    </option>
                <?php endforeach; ?>
                <option value="full" <?php selected('full', $value); ?>>
                    <?php esc_html_e('Full (original image)', 'comment-image-boost'); ?>
                </option>
            </select>
            <?php if ($desc): ?>
                <p class="description"><?php echo esc_html($desc); ?></p>
            <?php endif; ?>
            <?php

        } catch (Exception $e) {
            ciboost_log('Error rendering image size field: ' . $e->getMessage());
        }
    }

    /**
     * Renderiza campo de layout de exibiÃƒÂ§ÃƒÂ£o
     */
    public function render_field_layout($args) {
        try {
            $options = get_option(self::OPTION_ID, array());
            $name = $args['name'];
            $is_free = !ciboost_can_use_premium_features();
            $value = isset($options[$name]) ? $options[$name] : 'vertical';
            $desc = isset($args['desc']) ? $args['desc'] : '';
            $pro_layouts = array('horizontal');

            $layouts = array(
                'vertical' => __('Vertical (one per line)', 'comment-image-boost'),
                'horizontal' => __('Horizontal (grid 4 columns)', 'comment-image-boost'),
            );

            if ($is_free) {
                $value = 'vertical';
            }

            ?>
            <select name="<?php echo esc_attr(self::OPTION_ID . '[' . $name . ']'); ?>">
                <?php foreach ($layouts as $layout_value => $layout_label):
                    $is_pro_option = $is_free && in_array($layout_value, $pro_layouts, true);
                ?>
                    <option value="<?php echo esc_attr($layout_value); ?>" <?php selected($layout_value, $value); ?> <?php disabled($is_pro_option); ?>>
                        <?php echo esc_html($layout_label); ?><?php if ($is_pro_option): ?><?php esc_html_e(' (PRO)', 'comment-image-boost'); ?><?php endif; ?>
                    </option>
                <?php endforeach; ?>
            </select>
            <?php if ($is_free): ?>
                <span class="ciboost-pro-badge">PRO</span>
            <?php endif; ?>
            <?php if ($desc): ?>
                <p class="description"><?php echo esc_html($desc); ?></p>
            <?php endif; ?>
            <?php

        } catch (Exception $e) {
            ciboost_log('Error rendering layout field: ' . $e->getMessage());
        }
    }

    /**
     * Renderiza campo de posiÃƒÆ’Ã‚Â§ÃƒÆ’Ã‚Â£o da imagem.
     */
    public function render_field_gallery_layout($args) {
        try {
            $options = get_option(self::OPTION_ID, array());
            $name = $args['name'];
            $value = isset($options[$name]) ? $options[$name] : 'grid';
            $desc = isset($args['desc']) ? $args['desc'] : '';

            $layouts = array(
                'grid' => __('Grid', 'comment-image-boost'),
                'carousel' => __('Carousel', 'comment-image-boost'),
            );

            ?>
            <select name="<?php echo esc_attr(self::OPTION_ID . '[' . $name . ']'); ?>">
                <?php foreach ($layouts as $layout_value => $layout_label): ?>
                    <option value="<?php echo esc_attr($layout_value); ?>" <?php selected($layout_value, $value); ?>>
                        <?php echo esc_html($layout_label); ?>
                    </option>
                <?php endforeach; ?>
            </select>
            <?php if ($desc): ?>
                <p class="description"><?php echo esc_html($desc); ?></p>
            <?php endif; ?>
            <?php

        } catch (Exception $e) {
            ciboost_log('Error rendering gallery layout field: ' . $e->getMessage());
        }
    }

    public function render_field_position($args) {
        try {
            $options = get_option(self::OPTION_ID, array());
            $name = $args['name'];
            $value = isset($options[$name]) ? $options[$name] : 'after';
            $desc = isset($args['desc']) ? $args['desc'] : '';
            $positions = array(
                'after' => __('After comment text', 'comment-image-boost'),
                'before' => __('Before comment text', 'comment-image-boost'),
            );

            ?>
            <select name="<?php echo esc_attr(self::OPTION_ID . '[' . $name . ']'); ?>">
                <?php foreach ($positions as $position_value => $position_label): ?>
                    <option value="<?php echo esc_attr($position_value); ?>" <?php selected($position_value, $value); ?>>
                        <?php echo esc_html($position_label); ?>
                    </option>
                <?php endforeach; ?>
            </select>
            <?php if ($desc): ?>
                <p class="description"><?php echo esc_html($desc); ?></p>
            <?php endif; ?>
            <?php
        } catch (Exception $e) {
            ciboost_log('Error rendering position field: ' . $e->getMessage());
        }
    }

    /**
     * Renderiza campo de post types.
     */
    public function render_field_post_types($args) {
        try {
            $options = get_option(self::OPTION_ID, array());
            $name = $args['name'];
            $value = isset($options[$name]) && is_array($options[$name]) ? $options[$name] : array();
            $desc = isset($args['desc']) ? $args['desc'] : '';
            $post_types = get_post_types(array('public' => true), 'objects');

            ?>
            <fieldset>
                <?php foreach ($post_types as $post_type): ?>
                    <?php if (!post_type_supports($post_type->name, 'comments')) { continue; } ?>
                    <label style="display: inline-block; margin-right: 15px; margin-bottom: 8px;">
                        <input
                            type="checkbox"
                            name="<?php echo esc_attr(self::OPTION_ID . '[' . $name . '][' . $post_type->name . ']'); ?>"
                            value="1"
                            <?php checked(in_array($post_type->name, $value, true)); ?>
                        />
                        <?php echo esc_html($post_type->labels->singular_name); ?>
                    </label>
                <?php endforeach; ?>
            </fieldset>
            <?php if ($desc): ?>
                <p class="description"><?php echo esc_html($desc); ?></p>
            <?php endif; ?>
            <?php
        } catch (Exception $e) {
            ciboost_log('Error rendering post types field: ' . $e->getMessage());
        }
    }

    /**
     * Renderiza campo de categorias.
     */
    public function render_field_categories($args) {
        try {
            $options = get_option(self::OPTION_ID, array());
            $name = $args['name'];
            $value = isset($options[$name]) && is_array($options[$name]) ? array_map('absint', $options[$name]) : array();
            $desc = isset($args['desc']) ? $args['desc'] : '';
            $categories = get_categories(array('hide_empty' => false));

            ?>
            <fieldset style="max-height: 180px; overflow: auto; border: 1px solid #dcdcde; padding: 10px; background: #fff;">
                <?php foreach ($categories as $category): ?>
                    <label style="display: block; margin-bottom: 8px;">
                        <input
                            type="checkbox"
                            name="<?php echo esc_attr(self::OPTION_ID . '[' . $name . '][' . $category->term_id . ']'); ?>"
                            value="1"
                            <?php checked(in_array((int) $category->term_id, $value, true)); ?>
                        />
                        <?php echo esc_html($category->name); ?>
                    </label>
                <?php endforeach; ?>
            </fieldset>
            <?php if ($desc): ?>
                <p class="description"><?php echo esc_html($desc); ?></p>
            <?php endif; ?>
            <?php
        } catch (Exception $e) {
            ciboost_log('Error rendering categories field: ' . $e->getMessage());
        }
    }

    /**
     * Renderiza campo de formatos permitidos
     */
    public function render_field_formats($args) {
        try {
            $options = get_option(self::OPTION_ID, array());
            $name = $args['name'];
            $value = isset($options[$name]) ? $options[$name] : array('jpg', 'jpeg', 'png', 'webp', 'avif');
            $desc = isset($args['desc']) ? $args['desc'] : '';

            // Garante que ÃƒÂ© array
            if (!is_array($value)) {
                $value = array('jpg', 'jpeg', 'png', 'webp', 'avif');
            }

            $formats = array(
                'jpg' => 'JPG',
                'jpeg' => 'JPEG',
                'png' => 'PNG',
                'webp' => 'WebP',
                'avif' => 'AVIF'
            );

            $is_free = !ciboost_can_use_premium_features();
            $pro_formats = array('webp', 'avif');

            ?>
            <fieldset>
                <?php foreach ($formats as $format_key => $format_label):
                    $is_pro_format = $is_free && in_array($format_key, $pro_formats, true);
                ?>
                    <?php if ($is_pro_format): ?>
                        <label style="display: inline-block; margin-right: 15px;" class="ciboost-field-pro-locked">
                            <input type="checkbox" disabled />
                            <?php echo esc_html($format_label); ?>
                            <span class="ciboost-pro-badge">PRO</span>
                        </label>
                    <?php else: ?>
                        <label style="display: inline-block; margin-right: 15px;">
                            <input
                                type="checkbox"
                                name="<?php echo esc_attr(self::OPTION_ID . '[' . $name . '][' . $format_key . ']'); ?>"
                                value="1"
                                <?php checked(in_array($format_key, $value)); ?>
                            />
                            <?php echo esc_html($format_label); ?>
                        </label>
                    <?php endif; ?>
                    </label>
                <?php endforeach; ?>
            </fieldset>
            <?php if ($desc): ?>
                <p class="description"><?php echo esc_html($desc); ?></p>
            <?php endif; ?>
            <?php

        } catch (Exception $e) {
            ciboost_log('Error rendering formats field: ' . $e->getMessage());
        }
    }

    /**
     * ObtÃƒÂ©m tamanhos de imagem disponÃƒÂ­veis
     */
    private function get_image_sizes() {
        try {
            $standard_sizes = array('thumbnail', 'medium', 'medium_large', 'large');
            $sizes = array();

            foreach ($standard_sizes as $size) {
                $sizes[$size] = array(
                    'width' => get_option("{$size}_size_w"),
                    'height' => get_option("{$size}_size_h"),
                    'crop' => get_option("{$size}_crop")
                );
            }

            return array_merge($sizes, wp_get_additional_image_sizes());

        } catch (Exception $e) {
            ciboost_log('Error getting image sizes: ' . $e->getMessage());
            return array();
        }
    }

    /**
     * Sanitiza configuraÃƒÂ§ÃƒÂµes antes de salvar
     */
    public function sanitize_settings($input) {
        try {
            $sanitized = array();

            if (isset($input['enabled'])) {
                $sanitized['enabled'] = (bool) $input['enabled'];
            }

            if (isset($input['required'])) {
                $sanitized['required'] = (bool) $input['required'];
            }

            if (isset($input['allow_multiple'])) {
                $sanitized['allow_multiple'] = (bool) $input['allow_multiple'];
            }

            if (isset($input['max_size_mb'])) {
                $sanitized['max_size_mb'] = absint($input['max_size_mb']);
                if ($sanitized['max_size_mb'] < 1) {
                    $sanitized['max_size_mb'] = 1;
                }
                if ($sanitized['max_size_mb'] > 50) {
                    $sanitized['max_size_mb'] = 50;
                }
            }

            if (isset($input['allowed_formats']) && is_array($input['allowed_formats'])) {
                $allowed = array('jpg', 'jpeg', 'png', 'webp', 'avif');
                $sanitized['allowed_formats'] = array();
                foreach ($input['allowed_formats'] as $format => $value) {
                    if (in_array($format, $allowed) && $value == '1') {
                        $sanitized['allowed_formats'][] = $format;
                    }
                }
                // Garante que ao menos um formato estÃƒÂ¡ selecionado
                if (empty($sanitized['allowed_formats'])) {
                    $sanitized['allowed_formats'] = array('jpg', 'jpeg', 'png', 'webp', 'avif');
                }
            }

            if (isset($input['image_size'])) {
                $sanitized['image_size'] = sanitize_text_field($input['image_size']);
            }

            if (isset($input['preview_size'])) {
                $sanitized['preview_size'] = absint($input['preview_size']);
                if ($sanitized['preview_size'] < 80) {
                    $sanitized['preview_size'] = 80;
                }
                if ($sanitized['preview_size'] > 300) {
                    $sanitized['preview_size'] = 300;
                }
            }

            if (isset($input['display_layout'])) {
                $allowed_layouts = array('vertical', 'horizontal');
                $layout = sanitize_text_field($input['display_layout']);
                $sanitized['display_layout'] = in_array($layout, $allowed_layouts) ? $layout : 'vertical';
            }

            if (isset($input['lazy_loading'])) {
                $sanitized['lazy_loading'] = (bool) $input['lazy_loading'];
            }

            if (isset($input['save_to_media_library'])) {
                $sanitized['save_to_media_library'] = (bool) $input['save_to_media_library'];
            }

            if (isset($input['strip_metadata'])) {
                $sanitized['strip_metadata'] = (bool) $input['strip_metadata'];
            }

            if (isset($input['enable_lightbox'])) {
                $sanitized['enable_lightbox'] = (bool) $input['enable_lightbox'];
            }

            if (isset($input['enable_captions'])) {
                $sanitized['enable_captions'] = (bool) $input['enable_captions'];
            }

            if (isset($input['show_badge'])) {
                $sanitized['show_badge'] = (bool) $input['show_badge'];
            }

            if (isset($input['image_position'])) {
                $allowed_positions = array('before', 'after');
                $position = sanitize_text_field($input['image_position']);
                $sanitized['image_position'] = in_array($position, $allowed_positions, true) ? $position : 'after';
            }

            if (isset($input['gallery_columns'])) {
                $sanitized['gallery_columns'] = absint($input['gallery_columns']);
                if ($sanitized['gallery_columns'] < 1) {
                    $sanitized['gallery_columns'] = 1;
                }
                if ($sanitized['gallery_columns'] > 6) {
                    $sanitized['gallery_columns'] = 6;
                }
            }

            if (isset($input['gallery_layout'])) {
                $allowed_gallery_layouts = array('grid', 'carousel');
                $gallery_layout = sanitize_key($input['gallery_layout']);
                $sanitized['gallery_layout'] = in_array($gallery_layout, $allowed_gallery_layouts, true) ? $gallery_layout : 'grid';
            }

            if (isset($input['enabled_post_types']) && is_array($input['enabled_post_types'])) {
                $public_post_types = get_post_types(array('public' => true));
                $sanitized['enabled_post_types'] = array();

                foreach ($input['enabled_post_types'] as $post_type => $value) {
                    $post_type = sanitize_key($post_type);
                    if ($value == '1' && in_array($post_type, $public_post_types, true)) {
                        $sanitized['enabled_post_types'][] = $post_type;
                    }
                }
            } else {
                $sanitized['enabled_post_types'] = array();
            }

            if (isset($input['enabled_categories']) && is_array($input['enabled_categories'])) {
                $sanitized['enabled_categories'] = array();

                foreach ($input['enabled_categories'] as $term_id => $value) {
                    if ($value == '1') {
                        $sanitized['enabled_categories'][] = absint($term_id);
                    }
                }
            } else {
                $sanitized['enabled_categories'] = array();
            }

            if (!ciboost_can_use_premium_features()) {
                $sanitized['required'] = false;
                $sanitized['allow_multiple'] = false;
                $sanitized['enable_captions'] = false;
                $sanitized['enabled_categories'] = array();
                $sanitized['max_size_mb'] = min(isset($sanitized['max_size_mb']) ? $sanitized['max_size_mb'] : 2, 2);
                $sanitized['display_layout'] = 'vertical';
                $sanitized['gallery_layout'] = 'grid';
                $sanitized['allowed_formats'] = array_values(array_intersect(
                    isset($sanitized['allowed_formats']) ? $sanitized['allowed_formats'] : array('jpg', 'jpeg', 'png'),
                    array('jpg', 'jpeg', 'png')
                ));

                if (empty($sanitized['allowed_formats'])) {
                    $sanitized['allowed_formats'] = array('jpg', 'jpeg', 'png');
                }
            }

            return $sanitized;

        } catch (Exception $e) {
            ciboost_log('Error sanitizing settings: ' . $e->getMessage());
            return array();
        }
    }

    /**
     * Renderiza pÃƒÂ¡gina principal do comment-image-boost
     */
    public function render_main_page() {
        try {
            $this->render_settings_page();

        } catch (Exception $e) {
            ciboost_log('Error rendering main page: ' . $e->getMessage());
        }
    }

    /**
     * Renderiza pÃƒÂ¡gina de configuraÃƒÂ§ÃƒÂµes
     */
    public function render_settings_page() {
        try {
            if (!current_user_can('manage_options')) {
                return;
            }

            // Verifica se hÃƒÂ¡ mensagens
            // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- WordPress Settings API provides this redirect flag after saving options.
            if (isset($_GET['settings-updated']) && 'true' === sanitize_text_field(wp_unslash($_GET['settings-updated']))) {
                add_settings_error(
                    'ciboost_comment_images_messages',
                    'ciboost_comment_images_message',
                    __('Settings saved successfully.', 'comment-image-boost'),
                    'success'
                );
            }

            ?>
            <div class="wrap ciboost-admin-wrap">
                <div class="ciboost-admin-header">
                    <div class="ciboost-admin-header-content">
                        <div class="ciboost-admin-header-title">
                            <div class="ciboost-admin-logo-wrap">
                                <img src="<?php echo esc_url(CIBOOST_PLUGIN_URL . 'assets/logo.png'); ?>" alt="" class="ciboost-admin-logo">
                            </div>
                            <div>
                                <h1><?php echo esc_html(get_admin_page_title() ?: __('Comment Image Boost', 'comment-image-boost')); ?></h1>
                                <p class="ciboost-admin-subtitle"><?php esc_html_e('Configure image upload settings for WordPress comments', 'comment-image-boost'); ?></p>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="ciboost-admin-notices">
                    <?php settings_errors('ciboost_comment_images_messages'); ?>
                </div>

                <?php $this->render_pending_email_card(); ?>

                <div class="ciboost-admin-container">
                    <div class="ciboost-admin-main">
                        <?php if (!ciboost_can_use_premium_features()): ?>
                            <div class="ciboost-upgrade-card">
                                <h2><?php esc_html_e('Grow comments with photo proof', 'comment-image-boost'); ?></h2>
                                <p><?php esc_html_e('Upgrade to Pro to unlock multiple photo uploads, required image comments, WebP/AVIF, compression, larger uploads, and carousel galleries.', 'comment-image-boost'); ?></p>
                                <a class="button button-primary button-hero ciboost-upgrade-trigger" href="#" data-feature="multiple-images">
                                    <?php esc_html_e('Upgrade to Pro', 'comment-image-boost'); ?>
                                </a>
                            </div>
                        <?php endif; ?>

                        <div class="ciboost-settings-card">
                            <nav class="ciboost-tabs-nav">
                                <button type="button" class="ciboost-tab active" data-tab="upload"><?php esc_html_e('Upload', 'comment-image-boost'); ?></button>
                                <button type="button" class="ciboost-tab" data-tab="display"><?php esc_html_e('Display', 'comment-image-boost'); ?></button>
                                <button type="button" class="ciboost-tab" data-tab="pro"><?php esc_html_e('Pro Features', 'comment-image-boost'); ?></button>
                            </nav>

                            <form action="options.php" method="post">
                                <?php settings_fields(self::OPTION_ID); ?>

                                <div class="ciboost-tab-panel active" data-panel="upload">
                                    <?php do_settings_sections('ciboost_tab_upload'); ?>
                                </div>
                                <div class="ciboost-tab-panel" data-panel="display">
                                    <?php do_settings_sections('ciboost_tab_display'); ?>
                                </div>
                                <div class="ciboost-tab-panel" data-panel="pro">
                                    <?php if (!ciboost_can_use_premium_features()): ?>
                                        <div class="ciboost-pro-disclaimer">
                                            <span class="dashicons dashicons-lock"></span>
                                            <h3><?php esc_html_e('Pro Features', 'comment-image-boost'); ?></h3>
                                            <p><?php esc_html_e('Upgrade to Pro to unlock captions, gallery customization, post type & category filters, image compression, shortcodes, Gutenberg block, and Elementor widget.', 'comment-image-boost'); ?></p>
                                            <ul class="ciboost-pro-disclaimer-list">
                                                <li><span class="dashicons dashicons-yes-alt"></span> <?php esc_html_e('Image captions for visitors and moderators', 'comment-image-boost'); ?></li>
                                                <li><span class="dashicons dashicons-yes-alt"></span> <?php esc_html_e('Gallery columns and carousel layout', 'comment-image-boost'); ?></li>
                                                <li><span class="dashicons dashicons-yes-alt"></span> <?php esc_html_e('Post type and category targeting', 'comment-image-boost'); ?></li>
                                                <li><span class="dashicons dashicons-yes-alt"></span> <?php esc_html_e('Automatic image compression and resize', 'comment-image-boost'); ?></li>
                                                <li><span class="dashicons dashicons-yes-alt"></span> <?php esc_html_e('Shortcodes, Gutenberg block, and Elementor widget', 'comment-image-boost'); ?></li>
                                            </ul>
                                            <a class="button button-primary button-hero ciboost-upgrade-trigger" href="#" data-feature="filters">
                                                <?php esc_html_e('Upgrade to Pro', 'comment-image-boost'); ?>
                                            </a>
                                        </div>
                                    <?php else: ?>
                                        <?php do_settings_sections('ciboost_tab_pro'); ?>
                                    <?php endif; ?>
                                </div>

                                <?php submit_button(__('Save Settings', 'comment-image-boost'), 'primary', 'submit', true, array('class' => 'button-primary button-hero')); ?>
                            </form>

                            <?php if (ciboost_can_use_premium_features()): ?>
                            <div class="ciboost-compression-panel">
                                <form action="options.php" method="post">
                                    <?php
                                    settings_fields('ciboost_image_compression');
                                    do_settings_sections('ciboost_image_compression');
                                    submit_button(__('Save Compression Settings', 'comment-image-boost'), 'primary', 'submit', true, array('class' => 'button-primary button-hero'));
                                    ?>
                                </form>
                            </div>
                            <?php endif; ?>
                        </div>
                    </div>

                    <div class="ciboost-admin-sidebar">
                        <div class="ciboost-info-card">
                            <h3>
                                <span class="dashicons dashicons-info-outline"></span>
                                <?php esc_html_e('How to Use', 'comment-image-boost'); ?>
                            </h3>
                            <ol class="ciboost-steps-list">
                                <li>
                                    <strong><?php esc_html_e('Enable the feature', 'comment-image-boost'); ?></strong>
                                    <p><?php esc_html_e('Turn on "Enable Comment Images" to activate the functionality.', 'comment-image-boost'); ?></p>
                                </li>
                                <li>
                                    <strong><?php esc_html_e('Choose allowed formats', 'comment-image-boost'); ?></strong>
                                    <p><?php esc_html_e('Select which image formats users can upload (JPG, PNG, WebP, AVIF).', 'comment-image-boost'); ?></p>
                                </li>
                                <li>
                                    <strong><?php esc_html_e('Set file size limit', 'comment-image-boost'); ?></strong>
                                    <p><?php esc_html_e('Define maximum file size (1-50 MB) for uploaded images.', 'comment-image-boost'); ?></p>
                                </li>
                                <li>
                                    <strong><?php esc_html_e('Configure display options', 'comment-image-boost'); ?></strong>
                                    <p><?php esc_html_e('Choose image size, preview size, and layout (vertical or horizontal grid).', 'comment-image-boost'); ?></p>
                                </li>
                                <li>
                                    <strong><?php esc_html_e('Optional settings', 'comment-image-boost'); ?></strong>
                                    <p><?php esc_html_e('Enable multiple images per comment and/or make image upload required.', 'comment-image-boost'); ?></p>
                                </li>
                            </ol>
                        </div>

                        <div class="ciboost-info-card ciboost-features-card">
                            <h3>
                                <span class="dashicons dashicons-star-filled"></span>
                                <?php esc_html_e('Features', 'comment-image-boost'); ?>
                            </h3>
                            <ul class="ciboost-features-list">
                                <li><span class="dashicons dashicons-yes"></span> <?php esc_html_e('Drag & drop support', 'comment-image-boost'); ?></li>
                                <li><span class="dashicons dashicons-yes"></span> <?php esc_html_e('Image preview before upload', 'comment-image-boost'); ?></li>
                                <li><span class="dashicons dashicons-yes"></span> <?php esc_html_e('Multiple image support', 'comment-image-boost'); ?></li>
                                <li><span class="dashicons dashicons-yes"></span> <?php esc_html_e('Automatic compression & resize', 'comment-image-boost'); ?></li>
                                <li><span class="dashicons dashicons-yes"></span> <?php esc_html_e('Media Library thumbnails', 'comment-image-boost'); ?></li>
                                <li><span class="dashicons dashicons-yes"></span> <?php esc_html_e('EXIF/GPS metadata removal', 'comment-image-boost'); ?></li>
                                <li><span class="dashicons dashicons-yes"></span> <?php esc_html_e('Moderation dashboard and cleanup tools', 'comment-image-boost'); ?></li>
                                <li><span class="dashicons dashicons-yes"></span> <?php esc_html_e('Gutenberg block and Elementor widget', 'comment-image-boost'); ?></li>
                                <li><span class="dashicons dashicons-yes"></span> <?php esc_html_e('EXIF orientation auto-fix', 'comment-image-boost'); ?></li>
                                <li><span class="dashicons dashicons-yes"></span> <?php esc_html_e('Modern image formats (WebP, AVIF)', 'comment-image-boost'); ?></li>
                                <li><span class="dashicons dashicons-yes"></span> <?php esc_html_e('Responsive grid layouts', 'comment-image-boost'); ?></li>
                                <li><span class="dashicons dashicons-yes"></span> <?php esc_html_e('Secure file validation', 'comment-image-boost'); ?></li>
                            </ul>
                        </div>

                        <div class="ciboost-info-card">
                            <h3>
                                <span class="dashicons dashicons-shortcode"></span>
                                <?php esc_html_e('Shortcodes & Integrations', 'comment-image-boost'); ?>
                                <?php if (!ciboost_can_use_premium_features()): ?>
                                    <span class="ciboost-pro-badge">PRO</span>
                                <?php endif; ?>
                            </h3>
                            <?php if (!ciboost_can_use_premium_features()): ?>
                                <p class="description" style="background: #fff3cd; border-left: 4px solid #f59e0b; padding: 10px; margin: 0 0 12px 0; font-size: 13px;">
                                    <?php esc_html_e('Shortcodes, Gutenberg block, and Elementor widget are available with the Pro plan.', 'comment-image-boost'); ?>
                                    <a href="#" class="ciboost-upgrade-trigger" data-feature="filters" style="font-weight: 600;"><?php esc_html_e('Upgrade to Pro', 'comment-image-boost'); ?></a>
                                </p>
                            <?php endif; ?>
                            <p><code>[ciboost_gallery]</code></p>
                            <p><code>[ciboost_gallery post_id="123" limit="12" columns="3"]</code></p>
                            <p><code>[ciboost_recent_images limit="8"]</code></p>
                        </div>

                        <div class="ciboost-info-card">
                            <h3>
                                <span class="dashicons dashicons-admin-tools"></span>
                                <?php esc_html_e('Diagnostics', 'comment-image-boost'); ?>
                            </h3>
                            <?php $this->render_diagnostics(); ?>
                        </div>
                    </div>
                </div>
            </div>
            <?php
            // Render upgrade modal for free users
            if (!ciboost_can_use_premium_features() && function_exists('ciboost_render_upgrade_modal')) {
                ciboost_render_upgrade_modal();
            }
            ?>
            <?php

        } catch (Exception $e) {
            ciboost_log('Error rendering settings page: ' . $e->getMessage());
        }
    }

    /**
     * Renderiza diagnÃƒÆ’Ã‚Â³stico rÃƒÆ’Ã‚Â¡pido do ambiente.
     */
    private function render_diagnostics() {
        $upload_dir = ciboost_get_comment_images_upload_dir();
        $freemius_loaded = function_exists('ca_fs') && ca_fs();
        $checks = array(
            __('Upload directory writable', 'comment-image-boost') => wp_is_writable($upload_dir),
            __('GD image library', 'comment-image-boost') => extension_loaded('gd'),
            __('EXIF support', 'comment-image-boost') => function_exists('exif_read_data'),
            __('Freemius SDK loaded', 'comment-image-boost') => (bool) $freemius_loaded,
        );

        echo '<ul class="ciboost-diagnostics-list">';
        foreach ($checks as $label => $status) {
            $status_class = $status ? 'dashicons-yes' : 'dashicons-warning';
            echo '<li>';
            echo '<span class="dashicons ' . esc_attr($status_class) . '"></span>';
            echo esc_html($label);
            echo '</li>';
        }
        echo '</ul>';
        /* translators: %s: absolute upload directory path. */
        echo '<p class="description">' . esc_html(sprintf(__('Upload path: %s', 'comment-image-boost'), $upload_dir)) . '</p>';
    }
}

// Inicializa a classe admin
$GLOBALS['CIBOOST_Admin'] = new CIBOOST_Admin();
