<?php
// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}

// Settings page callback
if (!function_exists('explorexr_premium_clear_plugin_cache')) {
    function explorexr_premium_clear_plugin_cache() {
        // Clear specific transients
        delete_transient('explorexr_viewer_version_check');
        delete_transient('explorexr_license_cache');
        delete_option('explorexr_last_cdn_check');
        
        global $wpdb;
        // Clear ExploreXR related transients (core + premium + addons)
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Necessary for bulk cache clearing
        $deleted = $wpdb->query(
            $wpdb->prepare(
                "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s OR option_name LIKE %s OR option_name LIKE %s",
                $wpdb->esc_like('_transient_explorexr_') . '%',
                $wpdb->esc_like('_transient_timeout_explorexr_') . '%',
                $wpdb->esc_like('_transient_explorexr_premium_') . '%',
                $wpdb->esc_like('_transient_timeout_explorexr_premium_') . '%'
            )
        );
        
        // Clear post meta cache for all models
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Necessary for clearing model cache
        $models = $wpdb->get_col(
            "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'explorexr_model' OR post_type = 'explorexr_premium_model'"
        );
        
        foreach ($models as $model_id) {
            wp_cache_delete($model_id, 'post_meta');
            clean_post_cache($model_id);
        }
        
        // Clear WordPress object cache if available
        if (function_exists('wp_cache_flush')) {
            wp_cache_flush();
        }
        
        return (int) $deleted;
    }
}

function explorexr_settings_page() {
    // Check user capabilities
    if (!current_user_can('manage_options')) {
        wp_die(esc_html__('Sorry, you are not allowed to access this page.', 'explorexr-premium'));
    }
    
    // Process reset request
    if (isset($_POST['explorexr_reset_settings']) && isset($_POST['explorexr_reset_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['explorexr_reset_nonce'])), 'explorexr_reset_settings')) {
        // Define all options to reset
        $options_to_reset = array(
            // Loading options
            'explorexr_loading_display',
            'explorexr_loading_bar_color',
            'explorexr_loading_bar_size',
            'explorexr_loading_bar_position',
            'explorexr_percentage_font_size',
            'explorexr_percentage_font_family',
            'explorexr_percentage_font_color',
            'explorexr_percentage_position',
            
            // Model handling options
            'explorexr_large_model_handling',
            'explorexr_large_model_size_threshold',
            
            // Overlay options
            'explorexr_overlay_bg_color',
            'explorexr_overlay_bg_opacity',
            
            // System options
            'explorexr_model_viewer_version',
            'explorexr_max_upload_size',
            'explorexr_cdn_source',
            'explorexr_script_location',
            'explorexr_script_loading_timing',
            'explorexr_lazy_load_poster',
            'explorexr_lazy_load_model',
            
            // Premium loading options
            'explorexr_premium_loading_bar_color',
            'explorexr_premium_loading_bar_height',
            'explorexr_premium_loading_bar_position',
            'explorexr_premium_percentage_show',
            'explorexr_premium_percentage_precision',
            'explorexr_premium_percentage_suffix',
            'explorexr_premium_loading_text_show',
            'explorexr_premium_loading_text_content',
            'explorexr_premium_loading_text_position',
            'explorexr_premium_loading_text_color',
            'explorexr_premium_overlay_show',
            'explorexr_premium_overlay_color',
            'explorexr_premium_overlay_blur',
            'explorexr_premium_lazy_load_model',
            'explorexr_premium_script_location'
        );
        
        // Also reset any ExploreXR/Premium addon options while preserving license data
        $option_prefixes = array(
            'explorexr_',
            'explorexr_premium_',
            'explorexr-premium-'
        );
        $protected_prefixes = array(
            'explorexr_license_'
        );
        
        global $wpdb;
        $options_to_delete = array_fill_keys($options_to_reset, true);
        foreach ($option_prefixes as $prefix) {
            $like_prefix = $wpdb->esc_like($prefix) . '%';
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Necessary for bulk option reset
            $found_options = $wpdb->get_col(
                $wpdb->prepare(
                    "SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE %s",
                    $like_prefix
                )
            );
            
            foreach ($found_options as $option_name) {
                $is_protected = false;
                foreach ($protected_prefixes as $protected_prefix) {
                    if (strpos($option_name, $protected_prefix) === 0) {
                        $is_protected = true;
                        break;
                    }
                }
                if (!$is_protected) {
                    $options_to_delete[$option_name] = true;
                }
            }
        }
        
        foreach (array_keys($options_to_delete) as $option_name) {
            delete_option($option_name);
        }
        
        // Clear all related transients
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Necessary for bulk transient deletion
        $wpdb->query(
            $wpdb->prepare(
                "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s OR option_name LIKE %s OR option_name LIKE %s",
                $wpdb->esc_like('_transient_explorexr_') . '%',
                $wpdb->esc_like('_transient_timeout_explorexr_') . '%',
                $wpdb->esc_like('_transient_explorexr_premium_') . '%',
                $wpdb->esc_like('_transient_timeout_explorexr_premium_') . '%'
            )
        );
        
        // Clear caches to ensure fresh defaults load immediately
        explorexr_premium_clear_plugin_cache();
        
        echo '<div class="notice notice-success is-dismissible"><p><strong>All settings have been reset to default values!</strong> The page will reload in 2 seconds.</p></div>';
        echo '<meta http-equiv="refresh" content="2">';
    }
      // Process cache clearing
    if (isset($_POST['explorexr_clear_cache']) && isset($_POST['explorexr_clear_cache_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['explorexr_clear_cache_nonce'])), 'explorexr_clear_cache')) {
        $deleted = explorexr_premium_clear_plugin_cache();
        echo '<div class="notice notice-success is-dismissible"><p><strong>Cache cleared successfully!</strong> Deleted ' . (int)$deleted . ' transient(s) and cleared model cache.</p></div>';
    }
    
    // Process general settings form submission
    if (isset($_POST['explorexr_action']) && $_POST['explorexr_action'] === 'save_general_settings' && isset($_POST['explorexr_general_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['explorexr_general_nonce'])), 'explorexr_general_settings')) {
        // Process general settings fields
        if (isset($_POST['explorexr_model_viewer_version'])) {
            update_option('explorexr_model_viewer_version', sanitize_text_field(wp_unslash($_POST['explorexr_model_viewer_version'])));
        }
        if (isset($_POST['explorexr_default_tone_mapping'])) {
            $allowed_tone_mappings = array( 'aces', 'pbr-neutral', 'neutral', 'commerce', 'agx', 'filmic' );
            $tone_mapping = sanitize_text_field( wp_unslash( $_POST['explorexr_default_tone_mapping'] ) );
            if ( in_array( $tone_mapping, $allowed_tone_mappings, true ) ) {
                update_option( 'explorexr_default_tone_mapping', $tone_mapping );
            }
        }
        if (isset($_POST['explorexr_max_upload_size'])) {
            $max_upload = absint($_POST['explorexr_max_upload_size']);
            if ($max_upload > 0) {
                update_option('explorexr_max_upload_size', $max_upload);
            }
        }

        echo '<div class="notice notice-success is-dismissible"><p>General settings have been saved successfully!</p></div>';
    }
    
    // Get system information for the new System Info section
    $system_info = explorexr_get_system_info();
      // Set up header variables
    $page_title = 'ExploreXR Settings';
    $header_actions = '<a href="https://expoxr.com/explorexr/documentation/settings" target="_blank" class="button">
                        <span class="dashicons dashicons-book settings-icon"></span> Documentation
                      </a>';
    ?>
    <div class="wrap">
        <h1>ExploreXR Settings</h1>
        
        <!-- WordPress.org Compliance: This div.wp-header-end is required for WordPress to place admin notices properly -->
        <div class="wp-header-end"></div>
        
        <!-- ExploreXR Plugin Content -->
        <div class="explorexr-admin-container explorexr-settings-page">
        
        <?php include EXPLOREXR_PLUGIN_DIR . 'admin/templates/notifications-area.php'; ?>
        <?php include EXPLOREXR_PLUGIN_DIR . 'admin/templates/admin-header.php'; ?>
        
        <!-- General Settings -->
        <?php
        $card_title = 'General Settings';
        $card_icon = 'admin-settings';
        ob_start();
        ?>
        <form method="post" action="" id="explorexr-general-settings-form">
            <?php wp_nonce_field('explorexr_general_settings', 'explorexr_general_nonce'); ?>
            <input type="hidden" name="explorexr_action" value="save_general_settings">
            
            <!-- Get current option values -->
            <?php
            $model_viewer_version    = get_option('explorexr_model_viewer_version', '4.1.0');
            $default_tone_mapping    = get_option('explorexr_default_tone_mapping', 'aces');
            $max_upload_size = get_option('explorexr_max_upload_size', 50);
            $server_max = function_exists('explorexr_get_server_max_upload') ? explorexr_get_server_max_upload() : 50;
            ?>
            
            <table class="form-table" role="presentation">
                <tr>
                    <th scope="row">
                        <label for="explorexr_model_viewer_version">Model Viewer Version</label>
                    </th>
                    <td>
                        <select name="explorexr_model_viewer_version" id="explorexr_model_viewer_version">
                            <option value="4.1.0" <?php selected($model_viewer_version, '4.1.0'); ?>>v4.1.0 (Latest stable)</option>
                            <option value="3.3.0" <?php selected($model_viewer_version, '3.3.0'); ?>>v3.3.0</option>
                            <option value="3.2.0" <?php selected($model_viewer_version, '3.2.0'); ?>>v3.2.0</option>
                            <option value="3.1.1" <?php selected($model_viewer_version, '3.1.1'); ?>>v3.1.1</option>
                            <option value="3.0.0" <?php selected($model_viewer_version, '3.0.0'); ?>>v3.0.0</option>
                        </select>
                        <p class="description">The version of Google's Model Viewer library to use. Newer versions may have more features but could be less stable.</p>
                    </td>
                </tr>
                <tr>
                    <th scope="row">
                        <label for="explorexr_default_tone_mapping">Default Tone Mapping</label>
                    </th>
                    <td>
                        <select name="explorexr_default_tone_mapping" id="explorexr_default_tone_mapping">
                            <option value="aces" <?php selected($default_tone_mapping, 'aces'); ?>>ACES (legacy default, v3.x behaviour)</option>
                            <option value="pbr-neutral" <?php selected($default_tone_mapping, 'pbr-neutral'); ?>>PBR Neutral (Model Viewer v4.x default)</option>
                            <option value="neutral" <?php selected($default_tone_mapping, 'neutral'); ?>>Neutral (Khronos standard)</option>
                            <option value="commerce" <?php selected($default_tone_mapping, 'commerce'); ?>>Commerce</option>
                            <option value="agx" <?php selected($default_tone_mapping, 'agx'); ?>>AgX</option>
                            <option value="filmic" <?php selected($default_tone_mapping, 'filmic'); ?>>Filmic</option>
                        </select>
                        <p class="description">Site-wide fallback tone mapping. Model Viewer v4.0 changed the default from <code>aces</code> to <code>pbr-neutral</code>. Keeping <strong>ACES</strong> preserves the look of existing models. The Environment addon overrides this per-model when active.</p>
                    </td>
                </tr>
                <tr>
                    <th scope="row">
                        <label for="explorexr_max_upload_size">Max Upload Size (MB)</label>
                    </th>
                    <td>
                        <input type="number" name="explorexr_max_upload_size" id="explorexr_max_upload_size" 
                               value="<?php echo esc_attr($max_upload_size); ?>" class="small-text" min="1" max="<?php echo esc_attr($server_max); ?>"> MB
                        <p class="description">Maximum file size for 3D model uploads. Server limit: <?php echo esc_html($server_max); ?> MB.</p>
                    </td>
                </tr>
            </table>
            
            <!-- Add hidden fields to preserve settings when submitting general settings -->
            <input type="hidden" name="explorexr_preserve_settings" value="1">
            
            <?php submit_button('Save General Settings'); ?>
        </form>
        <?php
        $card_content = ob_get_clean();
        include EXPLOREXR_PLUGIN_DIR . 'admin/templates/card.php';
        ?>
        
        <!-- System Information -->
        <?php
        $card_title = 'System Information';
        $card_icon = 'performance';
        ob_start();
        ?>
        <p>Detailed information about your system environment:</p>
        
        <table class="widefat striped">
            <tbody>
                <tr>
                    <th>WordPress Version</th>
                    <td><?php echo esc_html($system_info['wp_version']); ?></td>
                </tr>
                <tr>
                    <th>PHP Version</th>
                    <td><?php echo esc_html($system_info['php_version']); ?>
                        <?php if (version_compare($system_info['php_version'], '7.2', '<')): ?>
                            <span class="explorexr-badge error">Outdated</span>
                        <?php else: ?>
                            <span class="explorexr-badge ar">OK</span>
                        <?php endif; ?>
                    </td>
                </tr>
                <tr>
                    <th>MySQL Version</th>
                    <td><?php echo esc_html($system_info['mysql_version']); ?></td>
                </tr>
                <tr>
                    <th>Server Software</th>
                    <td><?php echo esc_html($system_info['server_software']); ?></td>
                </tr>
                <tr>
                    <th>Max Upload Size</th>
                    <td><?php echo esc_html($system_info['max_upload_size']); ?> MB</td>
                </tr>
                <tr>
                    <th>PHP Memory Limit</th>
                    <td><?php echo esc_html($system_info['memory_limit']); ?></td>
                </tr>
                <tr>
                    <th>PHP Max Execution Time</th>
                    <td><?php echo esc_html($system_info['max_execution_time']); ?> seconds</td>
                </tr>
                <tr>
                    <th>PHP Post Max Size</th>
                    <td><?php echo esc_html($system_info['post_max_size']); ?></td>
                </tr>
                <tr>
                    <th>GD Library</th>
                    <td><?php echo wp_kses_post($system_info['gd_installed'] ? '<span class="explorexr-badge ar">Installed</span>' : '<span class="explorexr-badge error">Not Installed</span>'); ?></td>
                </tr>
                <tr>
                    <th>Imagick</th>
                    <td><?php echo wp_kses_post($system_info['imagick_installed'] ? '<span class="explorexr-badge ar">Installed</span>' : '<span class="explorexr-badge">Not Installed</span>'); ?></td>
                </tr>
                <tr>
                    <th>Model Viewer Version</th>
                    <td><?php echo esc_html($system_info['model_viewer_version']); ?></td>
                </tr>
                <tr>
                    <th>Model Viewer Source</th>
                    <td><?php echo esc_html($system_info['model_viewer_source']); ?></td>
                </tr>
                <tr>
                    <th>Active Theme</th>
                    <td><?php echo esc_html($system_info['theme_name']); ?> v<?php echo esc_html($system_info['theme_version']); ?></td>
                </tr>
                <tr>
                    <th>ExploreXR Version</th>
                    <td><?php echo esc_html(EXPLOREXR_VERSION); ?></td>
                </tr>
                <tr>
                    <th>Browser</th>
                    <td id="explorexr-user-agent"><?php echo esc_html(isset($_SERVER['HTTP_USER_AGENT']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_USER_AGENT'])) : 'N/A'); ?></td>
                </tr>
            </tbody>
        </table>
        
        <p class="submit">                <button type="button" id="explorexr-copy-system-info" class="button copy-system-info-button">
                    <span class="dashicons dashicons-clipboard settings-icon"></span> Copy System Information                </button>
        </p>
        
        <?php
        $card_content = ob_get_clean();
        include EXPLOREXR_PLUGIN_DIR . 'admin/templates/card.php';        ?>
        
        <!-- Reset Settings -->
        <?php
        $card_title = 'Reset Settings';
        $card_icon = 'image-rotate';
        ob_start();
        ?>
        <p>Use this option to reset all plugin settings to their default values. This will not delete your 3D models or uploaded files.</p>
        
        <?php
        $alert_message = '<p><strong>Warning:</strong> This action cannot be undone. All your customized settings will be lost.</p>';
        include EXPLOREXR_PLUGIN_DIR . 'admin/templates/info-alert.php';
        ?>
        
        <form method="post" onsubmit="return confirm('Are you sure you want to reset all settings to default values? This action cannot be undone.'));\">
            <?php wp_nonce_field('explorexr_reset_settings', 'explorexr_reset_nonce'); ?>
            <p class="submit">
                <input type="submit" name="explorexr_reset_settings" class="button button-secondary" value="Reset All Settings">
            </p>
        </form>
        <?php
        $card_content = ob_get_clean();
        include EXPLOREXR_PLUGIN_DIR . 'admin/templates/card.php';
        ?>
          <!-- Cache Management -->
        <?php
        $card_title = 'Model Viewer Cache';
        $card_icon = 'update-alt';
        ob_start();
        ?>
        <p>In some cases, clearing the model viewer cache can help resolve display issues with 3D models.</p>
        <form method="post" onsubmit="return confirm('Are you sure you want to clear the model viewer cache? This will not affect your saved settings.'));\">
            <?php wp_nonce_field('explorexr_clear_cache', 'explorexr_clear_cache_nonce'); ?>
            <p class="submit">
                <input type="submit" name="explorexr_clear_cache" class="button button-secondary" value="Clear Cache">
            </p>        </form>
        <?php
        $card_content = ob_get_clean();
        include EXPLOREXR_PLUGIN_DIR . 'admin/templates/card.php';
        ?>
          <!-- Import/Export Settings -->
        <?php
        $card_title = 'Import/Export Settings';
        $card_icon = 'migrate';
        ob_start();
        ?>
        <div class="explorexr-import-export-section">
            <?php 
            // Call the section callback manually to display the description
            echo '<p>' . esc_html__('Backup your ExploreXR settings or restore from a previous backup.', 'explorexr-premium') . '</p>';
            
            // Call the import/export callback manually to show the UI
            if (function_exists('explorexr_import_export_callback')) {
                explorexr_import_export_callback();
            } else {
                echo '<p class="description">Import/Export functionality is not available.</p>';
            }
            ?>
        </div>
        <?php
        $card_content = ob_get_clean();
        include EXPLOREXR_PLUGIN_DIR . 'admin/templates/card.php';
        ?>
        
        <!-- About -->
        <?php
        $card_title = 'About ExploreXR';
        $card_icon = 'info';
        ob_start();
        ?>        <div class="explorexr-about-section">
            <div class="explorexr-logo-container">
                <?php 
                // phpcs:ignore PluginCheck.CodeAnalysis.ImageFunctions.NonEnqueuedImage -- Plugin logo for admin interface
                printf('<img src="%s" alt="%s" class="explorexr-logo" loading="lazy">', 
                    esc_url(EXPLOREXR_PLUGIN_URL . 'assets/img/logos/explorexr-logo-dark.png'), 
                    esc_attr__('ExploreXR Logo', 'explorexr-premium')
                );
                ?>
            </div>
            <div>
                <p class="explorexr-version-info"><strong>Version:</strong> <?php echo esc_html(EXPLOREXR_VERSION); ?></p>
                <p class="explorexr-description">ExploreXR enhances your WordPress site with 3D model display capabilities, using Google's Model-Viewer technology.</p>
            </div>
        </div>
        <p>For documentation, support, and more information, please visit our website:</p>
        <p><a href="https://expoxr.com" class="button" target="_blank">Visit ExploreXR Website</a></p>
        <?php
        $card_content = ob_get_clean();
        include EXPLOREXR_PLUGIN_DIR . 'admin/templates/card.php';
        ?>
    
    <!-- ExploreXR Footer -->
    <?php include EXPLOREXR_PLUGIN_DIR . 'admin/templates/admin-footer.php'; ?>
    
        </div><!-- .explorexr-admin-container -->
    </div><!-- .wrap -->
    <?php
}

/**
 * Register general settings
 */
function explorexr_general_settings_register_settings() {
    // Register general settings with sanitization callbacks
    register_setting('explorexr_settings', 'explorexr_model_viewer_version', array(
        'sanitize_callback' => 'sanitize_text_field'
    ));
    register_setting('explorexr_settings', 'explorexr_max_upload_size', array(
        'sanitize_callback' => 'absint'
    ));
    
    // Add settings section
    add_settings_section(
        'explorexr_general_settings',
        esc_html__('General Configuration', 'explorexr-premium'),
        'explorexr_general_settings_callback',
        'explorexr-settings'
    );
    
    // Add settings fields
    add_settings_field(
        'explorexr_model_viewer_version',
        esc_html__('Model Viewer Version', 'explorexr-premium'),
        'explorexr_model_viewer_version_callback',
        'explorexr-settings',
        'explorexr_general_settings'
    );
    
    add_settings_field(
        'explorexr_max_upload_size',
        esc_html__('Max Upload Size (MB)', 'explorexr-premium'),
        'explorexr_max_upload_size_callback',
        'explorexr-settings',
        'explorexr_general_settings'
    );
}
add_action('admin_init', 'explorexr_general_settings_register_settings');





